Environment Variables
Computer operating systems use environment variables to store information that is relevant to a variety of programs running on that computer. Environment variables typically store configuration data, which can then be accessed and used by programs. Some environment variables are set by the operating system, but others can be modified by you.
Changing the value of an environment variable will change the behaviour of any program which uses that variable. This is particularly important to know as we continue exploring how to use the command line, because there are environment variables that impact the shell program.
Important variables for the shell
There are a few variables that are really important as they can change how the shell works. Knowing about these variables and what they do, will be useful in debugging issues when things are not working, as incorrectly set environment variables is a common cause for problems. The main variables relevant to the shell are:
Environment Variable | Purpose |
---|---|
PATH | The list of folders to search for programs run at the terminal. |
SHELL | The path to your default shell program. |
Programs to run (PATH)
The PATH is probably the most important environment variable. It contains a list of colon (:) separated paths that are searched for the commands you run in the shell. When you type a command, the shell looks in all of these folders to find the program you are trying to run.
We can access environment variables in the terminal by using the $
followed by the variable name.
Therefore, $PATH
is how we access the PATH environment variable.
To see the value of a variable, we can combine this with the echo
command, which prints data to the terminal.
Try running echo $PATH
in the terminal to see the value of your PATH environment variable.
The output should look something like “/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”.
Using which
to search the PATH
Action | Command | Description |
---|---|---|
Find a program | which | Outputs the path to a program if it can be found on the PATH. |
Unix provides a which
program that is used to get the path to a program. It searches each of the folders on the PATH and outputs the path to the program it finds.
Which shell? (SHELL)
There are many different shell programs you can use. The SHELL
variable contains the path to the shell that will be used when you open a new terminal.
Try running echo $SHELL
to see what your SHELL environment variable contains.
Environment variable commands
Let’s explore the commands you can use to work with environment variables. You can use these to check things like your PATH, or to change its value.
Action | Command | Description |
---|---|---|
Change your shell | chsh | Change your default shell to a new program. |
Export a value | export | Modify an environment variable, creating or updating the variable’s value. |
Delete a variable | unset | Removes the environment variable. |
Output a message | echo | Output text to the terminal. |
Output variables | env | Output all environment variables and their values. |
Change your shell (chsh
)
You can change your shell program using chsh
. Use the -s
option and provide the path to the new shell program you want to use. This must be one of the programs listed in the /etc/shells file.
When you run chsh
you will be prompted for your password, as the operating system wants to ensure it is you changing such an important setting.
Create or update a variable (export
)
The export
command allows you to assign a value to an environment variable. The command must be passed an argument that defines the variable to set and its new value. This is in the form variable=value. For example:
You can also use export
to add to a value of an existing variable. The following example extends the value of the message variable.
Delete a variable (unset
)
You can remove variables that are set using the unset
command or by setting the variable to an empty value.
Both of these commands will do the same thing:
Output the environment (env
)
You can list all of the environment variables using the env command. This can be useful to see what variables exist.
Output a message (echo
)
We have already been using echo
to output the values of our environment variables.
However, you can write any data to the terminal using this command.
Pass echo
any text and it will print it to the terminal.
Within the text, you can optionally inject values from variables.
Saving environment values
All of the commands above only have effect as long as the shell in which you entered them is open. This means that when you close the terminal, or open a new one, it will not have any of the changed or added environment variables. To retain your changes you need to add these commands to a text file that is launched when the terminal starts.
The exact file depends on the specific shell you are using. Two of the most common shells are Bash and Zsh. Bash will read the hidden .bashrc file from your home directory, while Zsh reads the hidden .zshrc file. The rc in this case stands for read command. A terminal’s rc file is read when you open a new terminal, and the commands within the file are run.
You can edit your terminal’s rc file to include any commands you want run when you open a new terminal.