The PATH is an environment variable that contains the search path (a colon-separated set of directories) for executing commands and scripts. The PATH variable can be set on a per-user basis or system-wide for all user accounts.
Check Current $PATH Variables
You can see your PATH by running the following echo command:
[ravi@linuxshelltips:~]$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Add Directory to $PATH Variable
To temporarily add a directory, for example, /opt/sysadmin/scripts
to your path, you can run the following command:
[ravi@linuxshelltips:~]$ PATH=$PATH:/opt/sysadmin/scripts [ravi@linuxshelltips:~]$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/sysadmin/scripts
To permanently set your PATH, open your ~/.bash_profile
or ~/.bashrc
shell start-up file and edit it as shown.
[ravi@linuxshelltips:~]$ vim ~/.bashrc
Append the following lines at the end of the file.
PATH="$PATH:/opt/sysadmin/scripts" $export PATH OR $export PATH="$PATH:/opt/sysadmin/scripts"
Note: The above method only works for your user account.
Permanently Set $PATH for All Users
To permanently set system PATH for all users on the system, append the following lines at the end of the /etc/profile
file. On some Linux distros, you can also use the /etc/bash.bashrc
file (if it exists)but it should be sourced in /etc/profile for changes in it to work.
PATH="$PATH:/opt/sysadmin/scripts" export $PATH OR $export PATH="$PATH:/opt/sysadmin/scripts"
Alternatively, instead of making changes directly in the /etc/profile
(which is not recommended), you can create a script (ending with the .sh
extension) under the directory /etc/profile.d
(if it exists) and make the changes in that script.
[ravi@linuxshelltips:~]$ sudo vim /etc/profile.d/set_system_path.sh
Append the following line in it:
export PATH="$PATH:/opt/sysadmin/scripts"
Next, source the ~/.bashrc
or /etc/profile
or /etc/bash.bashrc
(depending on the one you used), for the changes to take effect.
[ravi@linuxshelltips:~]$ source ~/.bashrc OR [ravi@linuxshelltips:~]$ source /etc/profile OR [ravi@linuxshelltips:~]$ source /etc/bash.bashrc
Then confirm that your PATH has been set correctly:
[ravi@linuxshelltips:~]$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/sysadmin/scripts:/opt/sysadmin/scripts
If you have any queries or comments, use the feedback form below to reach us. You can as well share any Linux shell tips with us, concerning this topic.