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
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
The directories in this variable are separated by : (colon). At present, the shell searches the following directories for the binary executable files:
• /sbin and
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
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:
$ echo $PATH
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 script.
This tutorial we have learned how to add a directory to PATH enviornment variable in Linux. Hope you enjoyed reading this and please leave your suggestion in the below comment section.