Defining Variables#
By default all variables are local and the can just be defined by an assignment. It is important to leave no whitespace around the =
. This would be a syntactic error. To define a global variable it has to be exported, either direct in an assignment or later by invoking export
VARNAME
. The export can be undone with export -n
:
MY_LOCAL_VAR=123 # defining a local variable
export MY_GLOBAL_VAR=123 # defining a global variable
export MY_LOCAL_VAR # this now also a global variable
export -n MY_LOCAL_VAR # now it is local again
If not otherwise stated, all variables are treated as strings. E.g. MY_LOCAL_VAR
is not interpreted as an integer. It is rarely needed but variables and their attributes can be declared explicitly:
declare FOO=bar # equivalent to FOO=bar
declare -i N=100 # N is now defined as integer
declare -x MY_GLOBAL_VAR # equivalent to export MY_GLOBAL_VAR
Sometime it is useful do delete a variable. An assignment VAR=""
or even VAR=
is not enough. This just assigns an empty string to VAR
, but VAR
is still defined. The command unset VAR
undefines VAR
.
Note
bash
provides a special way to inherit or inject variables to its child processes’ environments without setting them for the parent process. If variables are set on the same line they are inherited as environment to the child but do not alter the environment of the parent (i.e. the current shell)
LC_ALL=C MY_VAR=120 ./my_prog
In the previous example the (existing) environment variable LC_ALL
is not altered and (the non-existent variable) MY_VAR
does not become an environment variable in the current shell, but both will be seen as environment from my_prog
.
Inspecting Variables#
The attributes (including the scope) and the value of a variable can be checked with declare -p
, this command displays an equivalent definition of the variable using the declare
built-in command.
declare -p MY_LOCAL_VAR
declare -- MY_LOCAL_VAR="123" # -- can be ignored
declare -p MY_GLOBAL_VAR
declare -x MY_GLOBAL_VAR="123" # -x indicates a global variable
There are several ways to inspect variables, some are listed in the following table.
Command |
Effect |
---|---|
|
Print all environment variables |
|
Print all environment variables |
|
Print all shell variables (including functions) |
|
Print all environment variables declarations |
|
Print all environment variables declarations |
Using Variables#
Variables are referenced by their name preceding a $
symbol. There is no distinction in local and global variables. bash
will replace $VAR
by its value (or empty string if VAR
is undefined). Variables can be used inside strings and other expressions. They can even be used as commands themselves.
NAME=World
echo "Hello$NAME!" # prints/echoes "HelloWorld!"
OPTIONS="-l"
COMMAND="ls"
echo Now running $COMMAND $OPTIONS # echoes "Now running ls -l"
$COMMAND $OPTIONS # runs ls -l
Special variables#
Bash maintains a set of special variables. They are read-only and very useful in scripting.
Variable |
Meaning |
---|---|
|
Exit code of the last command |
|
The |
|
The name of the current shell or shell script |
|
The |
|
The number of arguments for the current shell script |
The exit code is useful to check for errors in a shell script. For example in the snippet below a directory foo
is created. But this may fail, for example because the directory already exists, in this case $?
will be equal to 1
and the script can react accordingly. If mkdir foo
succeeds, $?
will be equal to 0
.
mkdir foo
if [ $? -neq 0 ] ; then # if "$? is not equal to 0" then
# error handling
To illustrate the other special variables assume the following shell script my_script.sh
:
#!/bin/bash
echo "Hello, this is $0" # print out a greeting
if [ $# -neq 2 ] ; then # if $# is not equal to 2
echo "I need exactly 2 arguments"
exit 1 # exit with exit code 1
fi
echo "My arguments are $1 and $2"
exit 0 # exit with exit code 0
The example invocations show the values of the special variables. In particular $0
includes
./my_script.sh foo bar
Hello, this is ./my_script.sh
My arguments are foo and bar
some_path/my_script.sh foo bar baz
Hello, this is some_path/my_script.sh
I need exactly 2 arguments