Shell Variables and $PATH

The Linux Shell

Shell Variables and the Environment


Like most shells Bash can store text and numbers into variables for retrieval later. Within the shell, variables are set with commands similar to "hum=bug". This sets the variable hum to the text value of bug. To retrieve the stored information back you can specify the variable name prefixed with a "$":


john@john-desktop:~$ hum=bug
john@john-desktop:~$ echo hum
hum
john@john-desktop:~$ echo $hum
bug

Notice that it was only when the "$" sign was placed before the variable name that the contents of the variable was displayed. Also remember, when assigning variables, do not leave any spaces before and after the equals sign!

Shell variables are only visible in the shell that they were created in. Environmental variables are different in that they are passed to the child process when an external command is started. All environment variables are shell variables. However, shell variables are not environmental variables. The export command is used to create a environmental variable.






john@john-desktop:~$ hum=bug
john@john-desktop:~$ export hum
john@john-desktop:~$ echo $hum
bug
john@john-desktop:~$ export hum=bug
john@john-desktop:~$ echo $hum
bug

To display all environmental variables, you can issue the "export" command without any parameters. The "env" command issued with no parameters will also display the current environment. All shell variables and those that are environmental variables will be displayed.

In the following example we launch a child shell with bah. Notice once we have exited the child shell and returned to the parent shell that the variable "$bah" is not defined!


john@john-desktop:~$ env bah=hum bash
john@john-desktop:~$ echo $bah
hum
john@john-desktop:~$ exit
exit
john@john-desktop:~$ echo $bah

john@john-desktop:~$

If you need to remove a value from a variable, you can use the "unset" command. If you want to remove a variable from the environment but keep it on as a shell variable you would use the "export -n " option:


john@john-desktop:~$ export hum=bug
john@john-desktop:~$ export -n hum
john@john-desktop:~$ unset hum

In the above example we export hum as an environmental variable, we then remove hum from the environment so it becomes only a shell variable. And finally we delete it using the unset command.

$PATH - What is a PATH


Whenever you run a program from the command line, Bash actually searches through a list of directories looking for the program that you requested. For example when you run the "ls" command, Bash doesn't know where this command is located with out looking through a list of possible places. This list of places is actually stored in a variable called $PATH. This variable contains a colon separated list of directories to search. If you issue the command "echo $PATH" you will see a list of directories.


john@john-desktop:~$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

Your "$PATH" will probably be different to mine, however, each directory would be searched until the appropriate program was found. In my case, "ls" can be found in the "/usr/bin" directory.


john@john-desktop:/bin$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

If you need to add to your PATH, then you can do this from the command line in the following way: "PATH=$PATH:~mynewbindir". Now issue echo $PATH, you should now see your directory added to the end of the search list.


john@john-desktop:~$ PATH=$PATH:~mynewdir
john@john-desktop:~$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:~mynewbindir

Now "mynewbindir" has been added to the end of the list.

Whereis Which?


As we saw earlier, we can locate where a program is by issuing the "whereis" command. However, we can also issue the "which -a" command which will display all instances of the given program in your PATH variable.


john@john-desktop:~$ which -a ip
/sbin/ip
/bin/ip