What is a PATH variable
The PATH environment variable stores a colon separated list of locations to look for a command/application when one is run at the command line. For example, when running a command such as ls or vi the system checks all of the directories listed in the PATH (in order from left to right) to find the executable or script the user is attempting to run. This allows for running commands without knowing their location in the file system. Below is an example of PATH variable in Linux systems.
# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
By default, the PATH is already set to look in the following directories:
/usr/local/sbin /usr/local/bin /sbin /bin /usr/sbin /usr/bin
How to check the value of PATH variable
To check the current user’s path list, use any of the below commands:
# set | grep PATH PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
# env | grep PATH PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
Adding new directory to PATH variable For a specific user
A new directory can be added to a user’s PATH by editing ~/.bash_profile or ~/.bashrc files in the user’s home directory. For example, the PATH is normally set with lines similar to the following in ~/.bash_profile:
# vi ~/.bash_profile PATH=$PATH:$HOME/bin export PATH
To add a new directory to the path (e.g. ‘/new_path’), then change the PATH line by adding it to the end:
# vi ~/.bash_profile PATH=$PATH:$HOME/bin:/new_path ### Notice the colon ':' between the directories
Then copy the PATH and EXPORT lines from ~/.bash_profile to ~/.bashrc to ensure that the path gets set appropriately regardless of how the user logs into the machine. Following those changes the PATH will now include the directory ‘/programs’ the next time the user logs into the system.
Apply changes to current share
To apply the PATH for only the current bash terminal (without logging out), the comand below can be run:
$ . ~/.bash_profile
Adding new directory to the PATH variable for all users
The global path can be updated by either:
1. Adding a new file named /etc/profile.d/mypath.sh to be run upon login for all users, containing:
PATH=$PATH:/new_path
(Note: This method will affect all users (existing users and future users).
2. Editing the file named /etc/skel/.bash_profile in the same way discussed further above in this solution.
- The files in /etc/skel/ will be copied to any new user’s home directory upon their creation.
- Note: This method will not have an effect on any existing user accounts.