The Shell Environment
11 min read·Jan 1, 2025
In programming, a variable is a named container used to store data referred to as a value.
The shell environment, often abbreviated env, is a collection of variables in the form of name-value pairs.
It mostly contains information about your current shell session, which can be used by the shell itself or programs launched in your shell, in order to modify the way they run.
These variables often include information such as your username, the path of your home directory, the path to your shell, the path to your binaries, and so on.
The types of environment variables
Environment variables can be defined either by the operating system, users of the system, and sometimes, programs and scripts.
They can be local, which means that they are defined for the current shell session only and will be erased as soon as the shell is exited or the terminal is closed.
They can be user specific, which means that they are defined for a specific user in a configuration file that is loaded every time this user opens a shell.
They can be system wide, which means that they are available for every user of the system whenever a new shell session is started.
Common environment variables
The most common environment variables include:
USERwhich stores the username of the current user.HOMEwhich represents the user's home directory.PWDwhich represents the user's current working directory.PATHwhich specifies the directories where the shell looks for executable files.PS1which defines the character sequence of the shell prompt.
Displaying environment variables
To display all the environment variables and their values, you can use the env command as follows:
$ env
Example
This command will display all the environment variables present in the current shell session:
$ env
HOME=/home/rludosanu
LC_CTYPE=UTF-8
PATH=/usr/bin:/bin:/usr/sbin:/sbin/
PS1=$
PWD=/home/rludosanu
SHELL=/bin/bash
SHLVL=1
USER=razvan
Displaying a specific environment variable
To display the value of a specific environment variable, you can use the echo command as follows:
$ echo $variable
Where variable is the name of the environment variable preceded by a dollar sign $.
Example
This command will display the current value of the HOME environment variable containing the path to the home directory:
$ echo $HOME
/home/razvan
Defining environment variables
By convention, environment variable names are written in Screaming Snake Case, where each space is replaced with an underscore character (_) and words are written in uppercase.
ENV_VAR
Just like the arguments of shell command, the value of environment variables can be defined as single words or multiple words enclosed in quotes:
ENV_VAR=value
ENV_VAR="value"
ENV_VAR='value'
Setting command-specific variables
To declare a one-time use variable or temporarily overwrite the value of an existing one for a specific command, you can use the following syntax:
$ [variable=value ...] command
Where variable=value ... is a list of optional variable-value pairs separated by a space character.
Example
When executing the cd command without arguments, the shell will change the current working directory to the path specified in the HOME environment variable:
$ echo $HOME
/home/razvan
$ pwd
/home/razvan/learnbackend/
$ cd
$ pwd
/home/razvan
By temporarily overwriting the HOME environment variable, we can modify the behavior of the cd command so that when executed — this time only — the current working directory is changed to /home/razvan/Documents instead of /home/razvan, by default:
$ pwd
/home/razvan/learnbackend
$ HOME=/home/razvan/Documents cd
$ pwd
/home/razvan/Documents
$ cd
$ pwd
/home/razvan
Exporting local variables
It often happens that multiple commands require the same environment variable during the same shell session.
Rather than setting this variable for every single command, you can set them for the entire duration of the current shell session using the export command as follows:
$ export VARIABLE=value
Note: If a variable with the same name already exists in the environment, its value will be temporarily overwritten.
Example
This command will change the shell command prompt defined in the PS1 environment variable to include the username and the current working directory:
$ export PS1="\u:\w\$ "
razvan:~$
Where:
\uindicates the username of the current user.\windicates the current working directory.
Removing local variables
To remove a variable from the environment of the current shell session, you can use the unset command as follows:
$ unset VARIABLE
Example
This command will set the TMP environment variable to the "Hello World" string:
$ export TMP="Hello World"
$ echo $TMP
Hello World
This command will remove the TMP variable from the environment:
$ unset TMP
$ echo $TMP
$
Exporting user-specific variables
On Unix-like operating systems, users environments can be configured using the .bashrc and .bash_profile script files located in the home directory of the user, which are executed by the shell every time the user starts a new session, where:
- The
.bashrcscript is executed every time a new interactive non-login shell is started, which is a shell that doesn't require the user to log in using a username-password pair. - The
.bash_profilescript is executed only once when a new interactive login shell is started, which is a shell that requires the user to log in to the system, either locally at the console or remotely via SSH.
Note: macOS doesn't follow this convention and will run the
.bash_profilescript first instead of.bashrc.
Example
To export user specific variables that will be loaded by the shell every time you start a new shell session, you can create a new file named .bashrc in your home directory if it doesn't already exist:
$ vim ~/.bashrc
Once opened, you can add the environment variables you want to load for your profile using the export command:
export HOME=/home/razvan/projects
export PS1="\u:\w\$ "
Once the changes made, you can save this file and load the specified environment variables into your current shell session using the source builtin as follows:
$ source ~/.bashrc
Writing a cross-platform shell configuration
To create a macOS shell configuration with cross-platform portability, you can write your configuration in the .bashrc file and add replace the content of the .bash_profile file with the following condition:
if [ -r ~/.bashrc ]; then
source ~/.bashrc
fi
This way every time the .bash_profile file is loaded, it will verify the existence of the .bashrc file and load its content upon shell startup using the source command.
Summary
Here's a summary of what you've learned in this lesson:
- The shell environment is a collection of variables in the form of name-value pairs used to modify the way the shell or programs run.
- Environment variables can be local, user specific, and system wide.
- The
envcommand is used to display all the environment variables of the current shell session. - The
echo $variablecommand is used to display the value of a specific environment variable. - The
exportcommand is used to set or update an environment variable in the current shell session. - The
unsetcommand is used to remove an environment variable from the current shell session. - The
.bashrcfile is a script executed every time a new interactive non-login shell is started. - The
.bash_profilefile is a script executed every time a new interactive login shell is started.
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery