If you have been using Linux for some time, you would know that if you place a bash script in
/usr/sbin , they can be executed directly by simply executing the name of the script. Similarly, all the default commands in Linux do not need a path to execute and Linux simply knows where a certain command script is located. In this tutorial, we will see why that is the case and how we can change the $PATH.What is $PATH?
Usually, when you have to execute a script, you have to provide the full path (location) of the script to execute it. However, as I mentioned earlier, we use commands without mentioning the full path all the time. This is because of the $PATH environment variable. The $PATH variable has a certain path mentioned in it and when you execute a command, Linux checks for the corresponding command script in all those locations.
How to Check Your $PATH Variable?
To check your $PATH environment variable, simply execute the echo command:
In my path, I have
/home/ajax/.local/bin /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin/sbin/bin /usr/games /usr/local/games /snap/bin
which means it checks all the locations for scripts, every time I execute a command.
To see what path command is using, you can use the
How To Change The $PATH?
Changing $PATH is very useful since it enables us to call scripts from any directory without mentioning the full path. For example, suppose you have a directory
/example/scripts/ that is full of scripts you wish to execute. To avoid mentioning the full path every time you execute them you can add them to a directory that is already added to $PATH (such as /usr/bin) or you can add
/example/scripts to the $PATH itself and later remove it.
A directory can be added to $PATH temporarily or permanently.
Add to $PATH Temporarily
To add a directory to the $PATH temporarily i.e current session execute the following.
/example/scripts with the directory, you want to add to the $PATH .
Add to $PATH Permanently
Any changes made by the earlier method get removed when you open a new instance of the terminal. There is actually no simple direct way to change the $PATH permanently. However, we can use a trick and put the above command in a file where it is executed every time we open a new terminal to make it seem permanent.
To do this we first need to find what shell we are working in. For that execute the following
Chances are that you are working in Bash (/bin/bash). In that case, we can put
export PATH=$PATH:/example/scripts in ~/.bashrc
You can do the same by putting the export command in
If you are working in zsh, you can put it in
~/.zshrc and for fish, the correct location to put the export command is
Permanently Change $PATH for All Users
The above method only works for the current user. To change $PATH for all users we need to edit /etc/profile.
/etc/profile and all the scripts in /etc/profile.d/ are system-wide profiles that are executed by the system every time we start a shell (terminal). So if you add the export command in /etc/profile just like we did in ~/.bashrc, it will be executed every time a shell is started by any user.
Remove from $PATH
If you added a directory to the $PATH permanently and want to remove it, you can simply remove the corresponding command from the config file (bashrc, zshrc, kshrc, fish.config). This way the shell won’t read this command every time it loads up and won’t add the directory to the $PATH.
In this article, we learned what $PATH is, its usefulness, and most importantly, how to make permanent changes to $PATH. This is a very useful skill in Linux administration.