How to add to bash $PATH permanently on Linux

I am a new Linux and bash shell user. How do I permanently set $PATH on Linux? How can I permanently add a directory to shell PATH on Linux?

You need to setup a variable called PATH. The search path for commands defined by $PATH shell variable. It is nothing but a colon-separated list of directories in which the shell looks for commands. The default path is system-dependent, and is set by the sysadmin who installs bash or operating system for you.

See current PATH settings

Type the following command:

echo $PATH
## OR ##
printf "%sn", $PATH

Sample outputs:

/home/vivek/perl5/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

How to correctly add a path to PATH?

The syntax is:

PATH=$PATH:/new/dir1/here
PATH=$PATH:/new/dir1/here:PATH=$PATH:/new/dir2/here/

OR

PATH=/new/dir1/here:$PATH
PATH=/new/dir1/here:/new/dir2/here/:$PATH

To add $HOME/bin/ directory to path type:

## ~/bin/ to be searched after all other directories ##
PATH=$PATH:$HOME/bin/

OR

## ~/bin/ to be searched before all other directories ##
PATH=$HOME/bin/:$PATH

The first syntax adds $HOME/bin/ at the end PATH. The second syntax adds $HOME/bin/ at the beginning of PATH.

Fig.01: Sample bash shell session

Permanently add a directory to shell PATH

To permanently add shell path edit ~/.profile ~/.bash_profile file:
$ vi ~/.profile
OR
$ vi ~/.bash_profile
Append path setting:

## ~/bin/ to be searched after all other directories ##
export PATH=$PATH:$HOME/bin/

Save and close the file. To reload changes immedialty without logout, enter:
$ source ~/.profile
OR
$ source ~/.bash_profile
Verify it:
$ echo $PATH

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.