Your Linux system uses environment variables to store information that the shell and other processes can access. These variables contain useful data about your user session and system configuration.
Exploring Basic Environment Variables
You can view the value of a specific variable by prefixing its name with a $ symbol. For example, run the following command:
echo $HOME
This command will display the path to your home directory, which might look something like /home/pete.
Now, try another one:
echo $USER
This will output your current username. But where does this information come from? It's stored in your shell's environment.
What Does env Do in Linux
To see all the environment variables currently set for your session, you can use the env command. The linux env command is a fundamental tool for inspecting your shell's configuration.
env
Running the env command will output a list of key-value pairs. Here is a short example of what you might see:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/bin
PWD=/home/user
USER=pete
Understanding the linux env is crucial for managing your system effectively.
The Importance of the PATH Variable
One of the most important variables in your env linux output is PATH. You can view its contents specifically with:
echo $PATH
This command returns a colon-separated list of directories. When you type a command, your system searches through these directories to find the corresponding executable file.
Imagine you manually install a program in a non-standard directory like /opt/coolapp/bin. If you try to run it by typing coolcommand, you might get a "command not found" error. This happens because the directory containing your program is not listed in the PATH variable, so the shell doesn't know where to look for it.
To fix this, you can modify the PATH variable to include the new directory. By adding your custom directory to PATH, you enable the shell to find and execute your programs from anywhere in the terminal.
Setting an Environment Variable for the Current Session
Running the following command in your terminal sets the environment variable TEST for the current session only:
export TEST=test
After this, if you run:
echo $TEST
The output will be:
test
This variable will be available as long as the terminal session remains open. Once you close and reopen the terminal, the variable will no longer exist.
Making the Environment Variable Persistent Across Sessions
If you want the environment variable to be available in every terminal session (even after closing and reopening the terminal), you need to add it to your shell’s startup file. In the case of Bash (the default shell for many Linux distributions and macOS), this file is usually .bashrc in your home directory.
Here's how you do it:
- Open
.bashrcin your preferred text editor. For example:
nano ~/.bashrc
- Add the
exportline to the end of the file:
export TEST=test
-
Save and exit the editor (in Nano, this would be
Ctrl+X, thenYto confirm, andEnter). -
To apply the changes immediately without reopening the terminal, run:
source ~/.bashrc
After this, the TEST variable will be available in all future terminal sessions, and running echo $TEST will print test even after you close and reopen the terminal.
A Note on Shell Configuration Files
- For Bash (the default on many systems), the relevant file is
~/.bashrcfor non-login interactive shells. - For Zsh, the equivalent file is usually
~/.zshrc. - For Fish, you'd typically use
~/.config/fish/config.fish.