Environment Variables (Shell Scripting)

Environment Variables are a type of global variable in shell scripts that are always available to the programmer. They are summarized in the following table:

Environment Variable Variable Description
$HOME The home directory of the current user.
$PATH A semi-colon delimeted list of all paths that the shell will search when looking for an executable file.
$PS1 A variable containing the symbol(s) that will be shown at the primary command prompt
$PS2 A variable containing the symbol(s) that will be shown at the secondary command prompt
$IFS A list of characters that the shell considers as delimeters when reading parameters to a program call. Usually contains a space, a tab, and a newline character.
$0 The name of the current shell script, in the context of how it was invoked.
$# The number of parameters passed to the current shell script
$1-$[#] The parameters passed into the program.
$$ The PID (process ID) of the current script.

The following code demonstrates the use of some of these variables:


echo "You provided $# parameters to this program"
echo "This process's PID is $$"
echo "Your home folder is: $HOME"

The output of the following program is:

$ ./snippetProgram.sh cool beans
You provided 2 parameters
This process's PID is 9002
Your home folder is /home/stargazer
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License