Linux bash shell provides a lot of information into running applications. PATH
is one of the most important ones which is used to locate binary files and libraries. In some situations, we may need to edit, add or remove some paths and locations from the bash PATH variable. In this tutorial, we will differently use cases about these operations.
Print Current PATH Variable
We will start printing the current PATH
variable. This will print currently available paths in the PATH
variable.
$ echo $PATH

PATH Variable Syntax
As we can see in previous example paths are stored in a single line and delimited with :
. Each entry is a separate path to search binaries and libraries.
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bi
Add New Path
Now we need to add a new path to the PATH
variable. We will put a delimiter which is :
and then put the new path we want to add. In this example, we will add /home/ismail/bin
as new path.
$ PATH=$PATH:/home/ismail/bin

Remove Existing Path
In order to remove the existing path, we should copy the PATH variable value and then remove the path we want to remove. Then set the new PATH variable.
Export PATH Variable
Newly created PATH variable will be available for the current shell sessions. If we need to make this available for all other sessions we should export PATH variable to all other sessions with export
command like below.
$ export PATH
Make PATH Variable Persistent
Even we export our PATH variable this will not make our variable persistent after a reboot all newly added paths will be removed. In order to make the PATH variable persistent, we should add it to .bashrc
file which will read before a shell start for the current user.

Make PATH Variable Persistent and Available For All Users
In the previous example, the PATH variable will be available for only the current user. If we need to make it available for all other system users we should change the system-wide file /etc/profile
with a text editor. In order to change profile
file, we need root privileges.
