Here’s how to add a directory to your $ PATH on Linux

$PATH is one of the silent manipulators in the background of your Linux computer. It quietly affects your user experience, but there’s nothing shady about it. We explain what it does and how you can customize it.

What is $ PATH on Linux and how does it work?

When you type a command in a terminal window and press Enter, start quite a bit of activity before your command even runs.

Bash is the standard shell of most Linux distributions. It interprets the line of text you entered and identifies the command names associated with the parameters, pipes, Redirects, and what else is there. It then looks for the executable binaries for these commands and starts them with the parameters you specified.

The first step the shell takes to find the executable is to determine if there is any binary involved at all. If the command you are using is in the shell itself (a “Built-in shell”) no further search is required.

Shell build-ins are the easiest to find because they are an integral part of the shell. It’s like having them in a tool belt – they’re with you always.


However, if you need any of your other tools, you will have to browse the workshop to find it. Is it on your workbench or a wall hanger? That’s what $PATH Environment variable does. It contains a list of the places the shell searches and the order in which it is searched.

If you want to see if a command a Shell builtin, an alias, a function or a stand-alone binary file mv / work / unfile, you can use the … type Command as shown below:

type clear
type cd

That tells us that clear is a binary file, and the first one found in the path is under /usr/bin. You may have more than one version of the clear installed on your computer but the shell is trying to use it.

Not surprising, cd is a built-in shell.

List your $ PATH

It’s easy to see what’s in your way. Just enter the following to get the echo command and print out the value kept in the $PATH Variable:

echo $PATH

The output is a list of colons (:) delimited file system locations. The shell will search the path from left to right, checking each file system location for a suitable executable to run your command.

We can navigate through the list to see the file system locations to search and the order in which they will be searched:

  • /usr/local/sbin
  • /usr/local/bin
  • /usr/sbin
  • /usr/bin
  • /sbin
  • /bin
  • /usr/games
  • /usr/local/games
  • /snap/bin


Something that may not be immediately obvious is that the search doesn’t start in the current working directory. Rather, it works its way through the listed directories and only the listed directories.

If the current working directory is not in your path, it will not be searched. If you have stored commands in directories that are not in the path, the shell will not find them either.

To demonstrate this we have a little program called. created rf. When executing, rf outputs the name of the directory from which it was started in the terminal window. It is located in /usr/local/bin. We also have a newer version in the /dave/work Directory.

We enter the following which command to show us which version of our program The shell finds and uses:

which rf

The shell reports that the version found is the one in the directory that is in the path.

We enter the following to get it started:

rf

Version 1.0 of rf runs and confirms our expectations were correct. The version found and executed is in /usr/local/bin.


To get a different version of the. to execute rf On this computer we need to use the path to the executable in the command line as shown below:

./work/rf

Now we’ve told the shell where to find the version of rf we want to run it uses version 1.1. If we prefer this version, we can convert it to the /usr/local/bin Directory and overwrite the old one.

Let’s say we’re developing a new version of the rf. We have to run it a lot as we develop and test it, but we don’t want to copy an unreleased development build to the live environment.

Or maybe we have a new version of. downloaded rf and would like to run some verification tests on it before we make it publicly available.

When we add our working directory to the path, we cause the shell to find our version. And this change only affects us – others continue to use the version of rf in /usr/local/bin .

Add a directory to your $ PATH

You can use the … export Command to add a directory to the $PATH. The directory is then added to the list of file system locations that the shell searches. When the shell finds a suitable executable, it stops searching. So you should make sure that it scans your directory first before /usr/local/bin.


It is very easy to do. For our example, let’s enter the following to add our directory to the beginning of the path so that it is the first place we’re looking for:

export PATH=/home/dave/work:$PATH

This command sets $PATH be the same as the directory we add /home/dave/work, and then the entire current path.

The first PATH does not have a dollar sign ($). We set the value for PATH. The final one $PATH has a dollar sign because we’re referring to the content that is in the PATH Variable. Also note the colon (:) between the new directory and the $PATH Variable names.

Let’s see what the path looks like now:

echo $PATH

Our /home/dave/work Directory is added at the beginning of the path. The colon we provided separates it from the rest of the path.

We’re entering the following to get our version of the. to check rf is the first one found:

which rf

The proof in the pudding is running rf, As shown below:

rf

The shell finds version 1.1 and executes it /home/dave/work.


To add our directory to the end of the path, let’s just move it to the end of the command like this:

export PATH=$PATH:/home/dave/work

Make the changes permanent

As Beth Brooke-Marciniak said, “Success is good, but success is fleeting.” The moment you close in the terminal window, any changes you made to the $PATH are away. To make them permanent, you have to have yours export Command in a configuration file.

If you have the export Command in your .bashrc File, the path is set every time you open a terminal window. not how SSH Meetings, for which you need to register, these are called “interactive” sessions.

In the past, you would export Command in your .profile File to set the path for logging into terminal sessions.

However, we have found that when we do the export Command in either .bashrc or .profile Files, the path is set correctly for both interactive and logon terminal sessions. Your experience may be different. In order to master all eventualities, we will show you how to do it in both files.

Use the following command in your /home Directory for editing the .bashrc File:

gedit .bashrc

the gedit Editor opens with the .bashrc File loaded.

The gedit editor with the


Scroll to the bottom of the file, then add the following export command that we used before:

export PATH=/home/dave/work:$PATH

Save the file. Next either close and open the terminal window again or use the dot Command to read the .bashrc File as follows:

. .bashrc

Then enter the following echo Command to check the path:

echo $PATH

This adds the /home/dave/work Directory at the beginning of the path.

The process of adding the command to the .profile File is the same. Enter the following command:

gedit .profile

the gedit Editor starts with the .profile File loaded.

The gedit editor with the loaded

add the export Command to the end of the file and then save it. Closing and opening a new terminal window is not enough to open the .profile File to be reread. For the new settings to take effect, you must log out and log back in or the dot Command as shown below:

. .profile

How to edit text files graphically on Linux with gedit

Paving the way for everyone

To set the path for all users of the system, you can use the /etc/profile File.

You have to use sudo, as follows:

sudo gedit /etc/profile


If that gedit Editor starts, add the export command to the end of the file.

The gedit editor with the loaded file

Save and close the file. The changes will take effect for others the next time they log in.

A note on safety

Make sure you don’t accidentally add a leading colon.:“To the path as shown below.

If you do this, the current directory will be searched first, which is a security risk. Suppose you downloaded an archive file and unzipped it to a directory. You look at the files and see another zipped file. You call unzip again to unzip this archive.

If the first archive has an executable named unzip that was a malicious executable, you would accidentally launch it instead of the real one unzip executable. This would happen because the shell would first look in the current directory.

So always be careful when using your. input export Commands. Use echo $ PATH to check them out and make sure they are how you want them to be.

Linux commands
Filestar · pv · cat · tac · chmod · grep · difference · sed · With · man · pushed · popd · fsck · Test disk · seq · fd · pandoc · CD · $ PATH · awk · join · jq · wrinkles · unique · journalctl · tail · stat · ls · fstab · echo · fewer · chgrp · chown · Rev · look · Strings · Type · rename · Postal code · unzip · assemble · ummount · To install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · weather · Nano · mkdir · from · ln · Patch · Convert · rclon · Scraps · srm
Processesalias · screen · above · kind · renice · progress · strace · system · tmux · chsh · story · at · Batch · for free · which · dmesg · chfn · User mod · ps · chroot · xargs · tty · pinkie finger · lsof · vmstat · Time out · Wall · Yes sir · kill · sleep · sudo · it is · Time · groupadd · User mod · groups · lshw · switch off · start anew · Stop · switch off · passwd · lscpu · crontab · date · bg · fg
Networkingnetstat · Ring · Trace route · ip · ss · who is · fail2ban · bmon · she · finger · nmap · ftp · curl · wget · who · who am I · w · iptables · ssh-keygen · ufw

Best Linux Laptops for Developers and Enthusiasts

Related Posts