Every time you run a command on a Linux terminal, you are basically commanding the shell to run an executable program bearing the given name. Executable programs, including simple programs such as ls, mkdir, touch, and find, reside on special directories on the filesystem. In Linux, directories that accommodate such executable programs include:

/usr/sbin

/bin/usr/local/bin

/usr/local/bin

/bin

/usr/bin

/snap/bin

/sbin

/usr/games

/usr/local/games

A file with executable permissions in one of those directories can be executed from any location within the Linux terminal.

So, the question begs, how does the Linux shell know where to look for the programs? It doesn’t start the search from the current directory or anywhere random in the filesystem. The shell relies on the $PATH variable.

What is the $PATH variable?

$PATH is an environment variable that tells the shell where to locate the executable file. There are various directories defined in the $PATH variable. To display directories in your $PATH, run the command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/1-5.jpg" data-lazy- height="200" src="data:image/svg xml,” width=”789″>

To locate the directory where a command executable is located, use the which command as follows

For example, to locate where the executable for the pwd command, run the command:

From the output, we can see that the executable resides in the /bin directory. For the touch command, the executable resides in the /usr/bin directory.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/2-5.jpg" data-lazy- height="238" src="data:image/svg xml,” width=”791″>

How to add a directory to $PATH

Suppose you have a simple shell script called greetings.sh that prints “Hello World” placed in the /home/james/files directory. By default, the directory is not yet defined in the $PATH variable. To run the script from any location or directory, you need to specify the absolute path to the script. Now, this can be cumbersome and time-consuming.

To run the script globally (regardless of your location in the filesystem) without specifying the full path to the script, you need to add the directory containing the script to the $PATH variable using the syntax below.

$ export PATH=$PATH:/path/to/directory/with/file

In this case, the command will be:

$ export PATH=$PATH:/home/james/files

You should now be in a position to call or run the script from any directory within your Linux system without specifying the absolute path to the script as shown.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/3-5.jpg" data-lazy- height="383" src="data:image/svg xml,” width=”794″>

How to permanently add the directory to the $PATH variable

The path that we have just defined to $PATH is only temporary and doesn’t persist when you close the terminal or reboot your system. It only works in the current shell session. If you exit and launch another session, you will bump into the error as shown.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/4-5.jpg" data-lazy- height="168" src="data:image/svg xml,” width=”734″>

To persist the changes, define the $PATH variable in the ~.bashrc configuration file. To accomplish this, open the configuration file.

Then add the line as shown.

$ export PATH=”$PATH:/home/james/files”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/5-6.jpg" data-lazy- height="197" src="data:image/svg xml,” width=”670″>

After that, save and exit. To load the new changes, invoke the source command as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/6-3.jpg" data-lazy- height="184" src="data:image/svg xml,” width=”735″>

To verify the addition of the directory to $PATH, execute the command as shown.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/7-2.jpg" data-lazy- height="291" src="data:image/svg xml,” width=”890″>

Wrapping up

And there you have it, guys! We have managed to add the directory to $PATH on Linux successfully. As you have seen, it’s quite convenient and straightforward, especially if you will be calling the script or application regularly from the shell. The same commands will work for any Linux flavor.