The shell also implements a variety of conditional expressionsexpansions, including expansions with side effects:
Conditional errors:
###The colon In all of the conditional syntaxes, it can be seen that using a : makes unset variables and variables set to the empty string act in the same way. That's usually a good idea, because sometimes it is not easy to unset a variable. For example, in the local assignment syntax shown immediately above, it is easy to assign a child variable to the empty string, but not at all easy to remove a variable from the child environment:
$ export foo=Goodbye
$ foo= dash -c 'echo "${foo-Hello}", world'
, world
$ foo= dash -c 'echo "${foo:-Hello}", world'
Hello, world
$ echo "$foo"
Goodbye
###Conditional expansions as commands
Sometimes it is convenient to just be able to execute a conditional expansion for its side effect, without actually using it in a command. Technically this is not possible, but you can use the special builtin command :, which does nothing:
# Equivalent to:
# if [ -z "$x" ]; then x="default value"; fi
: "${x:=default value}"
# Equivalent to:
# if [ $# -lt 3 ]; then
# echo "Three arguments are required" 1>&2
# exit 1
# fi
: "${3?Three arguments are required.}"
###Variable assignments The shell provides a simple syntax for assigning a value to a scalar variable:
###The colon In all of the conditional syntaxes, it can be seen that using a : makes unset variables and variables set to the empty string act in the same way. That's usually a good idea, because sometimes it is not easy to unset a variable. For example, in the local###Extended assignment syntax shown immediately above, it is easy to assign a child variable to the empty string, but not at all easy to remove a variable from the child environment:
$ export foo=Goodbye
$ foo= dash -c 'echo "${foo-Hello}", world'
, world
$ foo= dash -c 'echo "${foo:-Hello}", world'
Hello, world
$ echo "$foo"
Goodbye
###Mutating assignmentssyntaxes
Finally, someSome shells have associative arrays, which support indexed assignments. Consult individual shell manuals for details on associative arrays, including how to declare them.
###Assignments in arithmetic evaluation
Within arithmetic evaluation, C-like assignment operators (=, *=, +=, etc.) are available, but they only perform arithmetic assignment. A Posix shell only has two arithmetic evaluation contexts: the arithmetic expansion syntax $((...)) and the for ((expr; expr; expr)) compound statement. (You can use the : special builtin as above to turn an arithmetic expansion into a statement.)
However, most shells also allow conditional statements ((...)) and will also use arithmetic evaluation in context requiring a number, such as array subscripts and assignment to variables declared to be integers. Details vary; consult shell manuals.
Arithmetic evaluation contexts need to be treated with caution because in an arithmetic evaluation context, variable expansions are reinterpreted as arithmetic expression, not just as integers. In some shells (bash, for example), this can be exploited with injection attacks. Best practice is to never use an untrusted variable in an arithmetic evaluation context.