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
$ 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.
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.
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
profilefile, we need root privileges.