shell variables

In this post we learn to manage environment variables in the shell. These variables are often needed by applications.

$ dollar sign

Another important character interpreted by the shell is the dollar sign $. The shell will look for an environment variable named like the string following the dollar sign and replace it with the value of the variable (or with nothing if the variable does not exist).

These are some examples using $HOSTNAME, $USER, $UID, $SHELL, and $HOME.

shellterminals

case sensitive

This example shows that shell variables are case sensitive!

casesensi

creating variables

This example creates the variable $MyVar and sets its value. It then uses echo to verify the value.

var

quotes

Notice that double quotes still allow the parsing of variables, whereas single quotes prevent this.

myvar

The bash shell will replace variables with their value in double quoted lines, but not in single quoted lines.

today

set

You can use the set command to display a list of environment variables. On Ubuntu and Debian systems, the set command will also list shell functions after the shell variables. Use set | more to see the variables then.

unset

Use the unset command to remove a variable from your shell environment.

unset

$PATH

The $PATH variable is determines where the shell is looking for commands to execute (unless the command is builtin or aliased). This variable contains a list of directories, separated by colons.

paths

The shell will not look in the current directory for commands to execute! (Looking for executables in the current directory provided an easy way to hack PC-DOS computers). If you want the shell to look in the current directory, then add a . at the end of your $PATH.

echopath

Your path might be different when using su instead of su – because the latter will take on the environment of the target user. The root user typically has /sbin directories added to the $PATH variable.

SU

env

The env command without options will display a list of exported variables. The difference with set with options is that set lists all variables, including those not exported to child shells.

But env can also be used to start a clean shell (a shell without any inherited environment).

The env -i command clears the environment for the subshell. Notice in this screenshot that bash will set the $SHELL variable on startup.

homes

export

You can export shell variables to other shells with the export command. This will export the variable to child shells.

varone

delineate variables

Until now, we have seen that bash interprets a variable starting from a dollar sign, continuing until the first occurrence of a non-alphanumeric character that is not an underscore. In some situations, this can be a problem. This issue can be resolved with curly braces like in this example.

super

unbound variables

The example below tries to display the value of the $MyVar variable, but it fails because the variable does not exist. By default the shell will display nothing when a variable is unbound.

myvarti

For any query or issue, feel free to discuss on http://discuss.eduguru.in