Linux adding folder to PATH

Problem

Needed to ran a bash script from any folder

Solution

Add the folder to the PATH.

  • View what is in the path
  • Add directory to the end of the PATH
  • View PATH after the change
  • Run the script from any directory from the command line

imela@whiscardz ~ $ echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/j
ava-8-oracle/jre/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin

imela@whiscardz ~ $ PATH=$PATH:/opt/DataGrip-2018.1.1/bin
imela@whiscardz ~ $ echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/j
ava-8-oracle/jre/bin:/usr/lib/jvm/java-8-oracle/bin:/usr/lib/jvm/java-8-oracle/db/bin:/usr/lib/jvm/java-8-oracle/jre/bin:/opt/DataGrip-2018.1.1/bin
imela@whiscardz ~ $ datagrip.sh

The methods we’ve used so far only sets the environment variable for your current shell session; when you logout or close the terminal window, your changes will be forgotten. If you want to set PATH to a certain value every time you log in or start a new shell session, you should add it to your bash startup script. Every time you start an interactive shell session, bash reads the following files in order (if they exist), and executes the commands inside of them:

/etc/profile
~/.bash_profile
~/.bash_login
~/.profile

If you want to add the folder for every user then add it to /etc/profile otherwise add it to any of the others in your home directory. I added it to ~/.profile


imela@whiscardz ~ $ vim .profile

#Add datagrip to the PATH variable
PATH=$PATH:/opt/DataGrip-2018.1.1/bin; export PATH

References

How to add directory to system path in Linux

How to correctly add a path to PATH?

UNIX / Linux: Set your PATH Variable Using set or export Command