About
A variable in bash is called a parameter.
There is three different type of parameter
- variables: referenced by a name (Ex: ${myVariable})
- positional parameters (ie command/script arguments): referenced by a number (Ex: ${1})
- auto-set parameters: a special symbol (Special Character) that has different special meanings and uses
A parameter can be then referenced by:
- a name,
- a number
- or a special symbol (Special Character)
A parameter is set if it has been assigned a value. (The null string is a valid value)
Management
See also Bash - Parameter Expansion
Check if set
- -z string has zero length
if [ -z ${var+x} ]; then
echo "var is unset";
else
echo "var is set to '$var'";
fi
where
- ${var+x} is a parameter expansion which evaluates to nothing if var is unset, and substitutes the string x otherwise.
or -n: string is not null
if [ -n ${var+x} ]; then
echo "var is set to '$var'";
else
echo "var is unset";
fi