4. Shell Parameters

Bash allows the storage of values by parameters. They can be denoted by a name, a number or a special character. A variable is parameter denoted with a name:

$ declare film="A Clockwork Orange"

The value of a variable is obeyed by parameter expansion:

$ echo $film
A Clockwork Orange

The declare command is optional:

$ verse="Oranges and lemons"

A variable can store a null value:

$ value=

The value of the variable undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion (except declared as integer) and quote removal:

$ value='~/$(date)/$((1+2))'
$ echo $value
~/$(date)/$((1+2))

Expansion would give:

/home/pa/Thu Oct  4 19:24:22 CEST 2012/3

Variables may have options. They are set by the declare command:

$ declare -irx maxint=2**32-1

The option -i treats declare to evaluate the value of maxint as arithmetic expression:

$ echo $maxint
4294967295

-r marks the variable as read only:

$ maxint=orange
bash: maxint: readonly variable

-x marks the variable for export to child processes:

$ env | grep maxint
maxint=4294967295

Concatenation appends content to the value of a variable by the "+=" operator:

$ verse+=" says the bells of St. Clement's"
$ echo $verse
Oranges and lemons say the bells of St. Clement's

For variables marked as integers the "+=" operator does an incrementation:

$ declare -i int=4
$ int+=4-2**3
$ echo $int
0

To unset a variable, except a read only variable, the unset command is used:

$ unset int
$ echo $int