Add a path to the PATH environment variable

suggest change

The PATH environment variable is generally defined in ~/.bashrc or ~/.bash_profile or /etc/profile or ~/.profile or /etc/bash.bashrc (distro specific Bash configuration file)

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/lib/jvm/jdk1.8.0_92/bin:/usr/lib/jvm/jdk1.8.0_92/db/bin:/usr/lib/jvm/jdk1.8.0_92/jre/bin

Now, if we want to add a path (e.g ~/bin) to the PATH variable:

PATH=~/bin:$PATH
# or
PATH=$PATH:~/bin

But this will modify the PATH only in the current shell (and its subshell). Once you exit the shell, this modification will be gone.

To make it permanent, we need to add that bit of code to the ~/.bashrc (or whatever) file and reload the file.

If you run the following code (in terminal), it will add ~/bin to the PATH permanently:

echo 'PATH=~/bin:$PATH' >> ~/.bashrc && source ~/.bashrc

Explanation:


This is a bit of code (run in terminal) that will check if a path is already included and add the path only if not:

path=~/bin            # path to be included
bashrc=~/.bashrc      # bash file to be written and reloaded
# run the following code unmodified
echo $PATH | grep -q "\(^\|:\)$path\(:\|/\{0,1\}$\)" || echo "PATH=\$PATH:$path" >> "$bashrc"; source "$bashrc"

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents