How to Permanently Set $PATH in Linux

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


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


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.

$export PATH
$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.

export  $PATH
$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/

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
[ravi@linuxshelltips:~]$ source /etc/profile
[ravi@linuxshelltips:~]$ source /etc/bash.bashrc

Then confirm that your PATH has been set correctly:

[ravi@linuxshelltips:~]$ echo $PATH


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.

Tutorial Feedback...
Was this article helpful? If you don't find this article helpful or found some outdated info, issue or a typo, do post your valuable feedback or suggestions in the comments to help improve this article...

Got Something to Say? Join the Discussion...

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.