The environment variables control the behavior of the shell. Environment variables are the variables that set the working environment of the shell. Some of the environment variables are USER, HOME, SHELL, PWD, SHELL, PS1, PS2, etc.
The HOME variable contains the path of home directory of the user. Similarly, the other variables contain other values needed for the operation of the shell. This tutorial discusses an important shell environment variable called PATH and how you can add values to this variable.
In this tutorial, I will show you how to add a directory to your PATH variable in Linux. The $PATH variable display a list of all directories where Linux commands or executable files reside.
Show Variable Value
The shell interprets the value of a variable by the "$" sign. To display the value of a variable, precede the variable with "$" sign. The "echo" command is used to display the value of the variable. Let us display the value of the variable HOME:
$ echo $HOME /home/raghu
What is PATH variable
The commands in Unix/Linux are the binary executable files. When you enter a command at the shell prompt, the binary file with that name is executed. So when a command is entered at the prompt, the shell searches for that binary file in some directories. These directories are listed in the PATH variable. Without the PATH variable, no command can be executed.
Now let’s show the current value of PATH variable.
$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
The directories in this variable are separated by : (colon). At present, the shell searches the following directories for the binary executable files:
• /usr/local/sbin • /usr/local/bin • /usr/sbin • /usr/bin • /sbin and • /bin
The order of the search path is also important. Suppose, you enter the ls command at the command line. Now at first, the shell searches /usr/local/sbin directory. If it does not find an executable file with that name in this directory, then it looks in the /usr/local/bin directory and then /usr/sbin, followed by /usr/bin and so on. If none of the directories specified in the PATH variable contains that file, then "command not found" error is displayed on the terminal.
Which command is used to find out the directory that contains a particular command. This command also uses the PATH variable to search for the command location. For example,
$ which ls /bin/ls
It means that when you type ls at the prompt and execute the ls command, then “/bin/ls” file is executed. In other words, location of ls command is “/bin/ls”. You can also find path of other commands with "which" command.
Add Directories to PATH
Now assume that you create your shell scripts in bin directory located in your home directory, i.e. in “~/bin” directory (~ represents home directory); and now you want to add this directory to your PATH variable as well, so that you do not have to specify the path to your script every time you run the script.
The value of a variable is changed with the syntax “variable=value”. But here, we want to add a directory to the PATH variable. We can’t just write “PATH=~/bin” because it will overwrite all the previous values. We want to preserve the existing values contained in the variable. So, we use value of the variable, i.e. $PATH, for assigning new value. If it sounds confusing, don't worry, it will be clear with the following command:
$ export PATH=$PATH:~/bin $ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin
The "/root/bin" directory (/root is root user's home directory) has been added to the path variable. Now you can execute your script as a command, without specifying the full path to the script. The export command ensures that the variables are passed to the child processes without affecting the existing environment variables.
Let's take another example where you like to add a directory (/home/tom/scripts) for a user named 'tom' that may contain his scripts or binaries. You can place custom script path at the front of the PATH if the system wants to find it first.
$ export PATH=$HOME/scripts:$PATH
$ export PATH=$/home/tom/scripts:$PATH
To remove a path from the $PATH variable from the current session there is no straightforward command. Just get the current values of $PATH variable using "echo $PATH" and export again after removing unwanted paths. Or simply start a new shell.
How to Set PATH permanent
To set up $PATH variable permanent, have to add the above-mentioned command to the respective configuration file that launches shell. The most common file is ~/.bashrc if you are using Bash. For zsh, add it to ~/.zshrc file.
You can appropriately use /etc/environment and /etc/profile which defines global environment variables.
Open ~/.bashrc file in your favorite text editor and run the following command:
And, to load variable to the current shell run source command, type:
$ source ~/.bashrc
In this tutorial, we have learned how to add a directory to the PATH environment variable in Linux. Hope you enjoyed reading this and please leave your suggestion in the below comment section.