In many programming languages, we use to create a variable, which must start with either letter, or underscore, but you cannot start a variable with numbers, or any special characters.
Create a variable key=value
(with or without quotes) but if you have spaces in your value then you must wrap the variable’s value in single or double quotes.
1
2
3
4
5
6
7
8
9
10
fname="Yafiz"
fname='Yafiz'
fname=Yafiz
# this will give you an error
fname=Yafiz Abraham
# correct way to use spaces value with single or double quotes
fname='Yafiz Abraham'
fname="Yafiz Abraham"
This is how you can call your variables.
1
2
3
echo $fname
echo "$fname"
echo ${fname}
But calling a variable within single quotes will never work because bash or CLI treats this as a simple text.
1
echo '$fname'
To set the default value or error message, run the following commands.
-
:-
will give you default value only ifmy_var
is empty. -
:?
will show you an error message only ifmy_var
is empty.
1
2
3
my_var='' # empty variable
echo ${my_var:-'my default value'}
echo ${my_var:?message}