Posted: . At: 10:29 AM. This was 2 years ago. Post ID: 16329
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



How to add a directory to your PATH on Linux.


Shell variables (also called environment variables) are placeholders for information and Bash maintains a suite of these to keep track of various settings. Have a look at what shell variables Bash knows about by typing the following:

┌──(john㉿DESKTOP-PF01IEE)-[/mnt/c/Users/Intel i5/Videos]
└─$ env | grep PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files (x86)/Bitvise SSH Client:/mnt/c/Users/Intel i5/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/Intel i5/AppData/Local/Programs/oh-my-posh/bin:/mnt/c/Users/Intel i5/AppData/Local/Programs/oh-my-posh/themes:/mnt/c/Users/Intel i5/AppData/Local/Programs/Microsoft VS Code/bin

The env command will print all of the directory paths required by your Linux system.

One of those listed variables is PATH, which tells Bash in which directories to look for commands that you type. As you can see, the format of the variable is a list of directories separated by colons. When you run a command, Bash will search these directories in order to find your command. Why is this handy to know? Say you installed some new programs to /usr/local/bin — which is a commonplace to store user space programs that won’t get mixed up in the Linux distribution hierarchy.

You might want to add this directory to your PATH so you don’t have to type /usr/local/bin/fancyprogram. You can do this as follows:

PATH=$PATH:/usr/local/bin

In fact, many distributions will add this to your user account PATHs automatically, so if you’re logged in as your standard user account you might already see this directory in your PATH. Login as root, and then run env. Chances are you won’t see ‘/usr/local/bin’ in your PATH. This might explain all those times you’ve been using a root account and found that running a program doesn’t work — when you know it works under your normal account. The default root PATH is very different from the standard user account PATH. What’s the deal with the $PATH we hear you say. Glad you asked. Variables are referenced in the shell by their name but preceded by ‘$’ to clarify for Bash that you want to reference the variable rather than taking the words literally. So, from our example above, $PATH represents (some say ‘expands to’) the current PATH as displayed by env, to which we are adding ‘/usr/local/bin’. Yes, it’s pretty nifty that we’re giving a new value to PATH (the ‘PATH=’ part) and referencing the current value of that variable at the same time. If you find this logic exciting, you should be a programmer! So, now you know how to set a new environment variable from the command line. But how about making the change permanent? Simple, add the line to your .bash_profile file. By the way, if you want to add a new directory to your path that is searched before other directories you add it, of course, in front like so:

PATH=/usr/local/bin:$PATH

To initialise any changes you can exit and login again or, alternatively, you can reload it with:

. .bash_profile

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.