Environment variables are used to store values that can be accessed by multiple programs and scripts on a Linux or Unix system. One of the most commonly used environment variables is the PATH variable, which stores the directories where the system searches for executable files. In this tutorial, we will learn how to set environment variables, specifically the PATH variable, permanently on Linux/Unix systems.
Understanding Environment Variables
Environment variables are key-value pairs that store information about the system and user settings. They can be used by programs and scripts to customize their behavior or to access specific resources. The PATH variable is a special type of environment variable that stores a list of directories where the system searches for executable files.
Setting Environment Variables Temporarily
You can set an environment variable temporarily using the export
command in the terminal. For example, to add a directory to the PATH variable, you can use the following command:
export PATH=$PATH:/path/to/dir
This will update the PATH variable for the current shell session only. When you exit the terminal or start a new session, the change will be lost.
Setting Environment Variables Permanently
To set an environment variable permanently, you need to add it to one of the system configuration files. There are several files where you can set environment variables, depending on your system and user settings:
/etc/environment
: This file stores system-wide environment variables that are applied to all users.~/.profile
,~/.bashrc
, or~/.zshrc
: These files store user-specific environment variables that are applied to the current user only.
To add a directory to the PATH variable permanently, you can edit one of these files and add the following line:
export PATH=$PATH:/path/to/dir
For example, to set the PATH variable for all users on an Ubuntu system, you can edit the /etc/environment
file as root:
sudo nano /etc/environment
Add the following line to the end of the file:
PATH=$PATH:/path/to/dir
Save and exit the editor.
To apply the changes immediately, you can run the following command:
source /etc/environment && export PATH
Alternatively, you can log out and log back in to apply the changes.
Setting Environment Variables for Specific Shells
If you are using a specific shell, such as Bash or Zsh, you may need to set environment variables in a different file. For example:
- For Bash, you can set environment variables in
~/.bashrc
. - For Zsh, you can set environment variables in
~/.zshrc
.
To set the PATH variable for a specific shell, you can add the following line to the corresponding file:
export PATH=$PATH:/path/to/dir
For example, to set the PATH variable for Bash, you can edit the ~/.bashrc
file and add the following line:
export PATH=/var/lib/gems/1.8/bin:/home/user/.bin:$PATH
Save and exit the editor.
Conclusion
In this tutorial, we learned how to set environment variables, specifically the PATH variable, permanently on Linux/Unix systems. We discussed the different files where you can set environment variables, including system configuration files and user-specific files. We also covered how to apply changes immediately and how to set environment variables for specific shells.
By following these steps, you can customize your system settings and make it easier to access executable files and resources.