In Linux your PATH is a list of directories that the shell will look in for executable files when you issue a command without a path. The PATH variable is usually populated with some default directories, but you can set the PATH variable to anything you like.

When a command name is specified by the user or an exec call is made from a program, the system searches through $PATH, examining each directory from left to right in the list, looking for a filename that matches the command name. Once found, the program is executed as a child process of the command shell or program that issued the command.

-Wikipedia

In this short tutorial we will discuss how to add a directory to your PATH and how to make the changes permanent. Although there is no native way to delete a default directory from your path, we will discuss a work around. We will end by creating a short script and placing it in our newly created PATH to demonstrate the benefits of the PATH variable.

Typical Errors Caused by a File NOT in Your PATH

When you try to execute a command that has an unknown location (no path specified) to the shell, it will start searching the directories listed in your PATH from left to right. If no command exists with the name you provided the shell will display the following error.

$ gocrazy
bash: gocrazy: command not found…

You can search the directories in your PATH using the “which” command. If you issue the which command with an argument that the shell cannot find in your path, it will display the following error.

$ which gocrazy
/usr/bin/which: no gocrazy in (/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/home/savona/.local/bin:/home/savona/bin)

Even if the file exists in one of the directories in your PATH, it must be executable to be found.

Display the PATH Variable Value

You can check your current path using echo to print the value of $PATH to the screen (stdout).

[[email protected] ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/savona/.local/bin:/home/savona/bin

Adding a Directory to Your PATH

If you want to add a directory to your path, you can use the export command. For example, if you wanted to add a directory called scripts that resides in your home directory you could add it like so:

export PATH=$PATH:/home/savona/scripts
  • export = Tells bash to make the environmental variable available to any child processes.
  • PATH= Tells bash you are setting the $PATH variable
  • $PATH = Places the current value of the PATH variable into the newly set variable.
  • : = Is a seperator or delimiter
  • /home/savona/scripts = Is the directory we are adding

Now you have added /home/savona/scripts to your PATH.

[[email protected] ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/savona/.local/bin:/home/savona/bin:/home/savona/scripts

Now the shell will look in /home/savona/scripts when I call an executable from the command line.

Using the export works great for a temporary change to the variable. The changes we made using export will not be available if we open a new shell, or reboot. Export only makes it available to the current shell, child processes, or shells that are spawn from the current shell (sub-shells).

Make a Persistent Change to the PATH variable

In order to make changes to the PATH variable persistent, we have to add it to a file. Here we will add it to the ~/.bash_profile file. This file is read every time you login, so the PATH variable will be set and ready.

NOTE: The tilde ( ~ ) is a special function meaning your home directory.

Open ~/.bash_profile in your favorite editor and add the same export line you entered above.

export PATH=$PATH:~/scripts

Save and close the file.

Alternatively, you can just echo the line into the file like so:

echo 'export PATH=$PATH:~/scripts' >> ~/.bash_profile

You have to source the bash_profile file if you want to make the changes available in the current shell.

$ source ~/.bash_profile

You only have to source the file in the current shell. If you open a new shell, log out and back in, or reboot the bash_profile will be read and your PATH will be set.

Remove a Directory From Your PATH

There is no built in way to edit the default directories in the PATH variable. If you want to delete a PATH that you added, simple remove it from the export line you put into bash_profile.

If you want to delete a default directory from your path you will have to use some wizardry. Here we use sed to remove a default path.

To remove /usr/sbin from your path:

PATH=$(echo "$PATH" | sed -e 's/:\/usr\/sbin//')

Example:

[[email protected] ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/savona/.local/bin:/home/savona/bin:/home/savona/scripts:/home/savona/.local/bin:/home/savona/bin:/home/savona/scripts:/home/savona/.local/bin:/home/savona/bin:/home/savona/scripts

[[email protected] ~]$ PATH=$(echo "$PATH" | sed -e 's/:\/usr\/sbin//')

[[email protected] ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/home/savona/.local/bin:/home/savona/bin:/home/savona/scripts:/home/savona/.local/bin:/home/savona/bin:/home/savona/scripts:/home/savona/.local/bin:/home/savona/bin:/home/savona/scripts

You can export this or add it to bash_profile just as we did when adding a directory.

Enjoy the Convenience of Your New PATH

Let’s create a basic script that will say hello, tell us the time and date and give us the weather on the command line.

First change to your home directory:

cd ~/

Copy and paste the following lines into your terminal to make the script.

cat <<EOL >> hello.sh
#!/bin/bash
echo "Hello $(whoami)"
echo "Today is $(date +%A" "%B" "%d" "%Y)"
wget -qO- wttr.in | head -7
EOL

Now set the script to executable:

chmod +x hello.sh

Let’s run our script.

Trying to run the script using just the name will throw an error, because the script is not in your PATH. The shell doesn’t know where the executable is.

$ hello.sh
bash: hello.sh: command not found…

You have a file in ~/hello.sh and you can run it by either giving the full path, or using ./ if you are in the same directory as the executable, like so (replace username with yours):

/home/username/hello.sh

or

./hello.sh

Example:

A screenshot showing the output of the hello script.

You can not just type hello.sh or use tab completion because your home directory is NOT IN YOUR PATH!

Let’s create a ~/scripts folder and copy the hello.sh script into it and see what happens. Remember we added the ~/scripts folder to your path earlier.

mkdir ~/scripts && cp hello.sh !$

Now you should be able to run the script by simply typing hello.sh because bash knows to look in the ~/scripts folder for executables. Additionally you should be able to use tab completion.

[[email protected] ~]$ hello.sh 
Hello savona
Today is Sunday March 03 2019
Weather report: Philadelphia, United States
Overcast
.-. 30..+33 °F
.-( )-. ↓ 4 mph
(___,_)___) 9 mi
0.0 in

Conclusion

I probably got a little long winded there, I apologize. But we demonstrated how to display and manipulate the PATH variable. We also showed you why adding something to your path is convenient.

I typically just use ~/.local/bin for all of my scripts. It keeps my home directory tidy and still allows me to call my scripts without using an absolute path.

How do you use the PATH variable? Let us know in the comments below.

Resources