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 "%s\n", $PATH

Sample outputs:


How to correctly add a path to PATH?

The syntax is:




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

## ~/bin/ to be searched after all other directories ##


## ~/bin/ to be searched before all other directories ##

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

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
$ 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
$ source ~/.bash_profile
Verify it:
$ echo $PATH

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 3 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • DJC Nov 17, 2016 @ 17:28

    worth adding a note about the danger of adding “.” to root profile …

  • netikras Nov 17, 2016 @ 19:10

    Nice topic. A few things I’d like to add though…
    PATH is used by pretty much all the shells, not just BASH. Also it is highly recommended to hardcode the PATH env in every single script (probably in the beginning).
    While being a very nice place to keep references to all our binaries it might be harmful if not used carefully. Say you got some malware in the system (yes, it happens in Linux too – I’ve learnt it the hard way) which creates some custom scripts and renames them to ‘uptime’, ‘date’ or whatever other usual Linux command and stores them, say, in /tmp/bin/. Then it replaces PATH with “/tmp/bin/${PATH}” and executes any executable using ‘uptime’ internally. And a distracted sysadmin accidentally left a setuid bit enabled on some root-owned script… this means that this malware can easily trigger its script with root permissions because that suid-enabled script uses, say, a command ‘uptime’ which originally resides in /usr/bin, but thanks to modified PATH it has been overriden with /tmp/bin/uptime. Now malware gets a full access to your system to do anything it likes. See this:

    netikras@netikras-xps ~ $ mkdir /tmp/bin
    netikras@netikras-xps ~ $ vim /tmp/bin/uptime
    netikras@netikras-xps ~ $ chmod +x /tmp/bin/uptime 
    netikras@netikras-xps ~ $ PATH="/tmp/bin:${PATH}" which uptime
    netikras@netikras-xps ~ $ 

    Not pretty, is it?

    Similar requirements apply to IFS as well. If one is writing a script one should either always hardcode PATH or always specify use full path to the executables (the former is much more practical). Hardcoded IFS always is a very good idea too.

    • Josh Nov 18, 2016 @ 16:44

      You sir, are a very smart man. Makes a lot of sense. @netikiras

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum