Linux Commands

How to Add a Directory to PATH in Linux

PATH is a predefined variable in a Linux shell. It consists of directories with their directory paths, beginning from the root. PATH is a colon-separated list. Each directory with its path is separated from the preceding directory with its path by a colon. The shell looks for commands in these directories.

Commands are not only disk operating system commands. The path is a list of directories in which the shell looks for operating system commands, and other commands. Commands are like short program files. So, commands are executables. Each application installed in the computer comes with an executable file. In order to run the application, the executable file is engaged first. These executable files are also called commands.

Without the PATH variable, each command would be commanded with the absolute path, as in,


where the first / is the root directory; john is the user directory for the user, John; dir1 and dir2 are sub-directories; and command.exe is the name of the executable file.

Many other names are possible for the executable file. In fact, command.exe is symbolic, here. So, if the directory, /home/john/dir1/dir2 (without the file), is in the PATH variable, the user john, will execute command.exe at the prompt, [email protected]:~$ by simply typing, command.exe, without the preceding path. That is:

[email protected]:~$ command.exe

and then pressing Enter.

In order to see the current list of PATH directories in the computer, type:

$ echo $PATH

and press Enter, at the terminal. The result would be something like,


The question now is: how is a directory (and its preceding path), added to the PATH variable? With some installations, the directory is added automatically. With others, it has to be added manually. This article explains the two main ways, in which the directory is added. The directory is not added (included) as it would be done to an array. The two main ways are referred to as Temporary Addition and Permanent Addition. Bash is the shell used for the code examples in this article.

Article Content

Temporary Addition

Temporary addition means the addition only takes place in memory. It is not saved for use when the computer is rebooted.

The Bourne Shell Built-in Export Command

In simple terms, the export command is:

$ export [name[=value]]

In this case, it reassigns a value to a variable in memory.

The PATH variable in memory may already have value like,


Assume that the directory to be added is /home/john/dir1/dir2. If the export command is typed as,

$ export PATH=/home/john/dir1/dir2

then /home/john/dir1/dir2 would replace all what is already in memory as value for the PATH variable.

If the export command is typed as,

$ export PATH=$PATH:/home/john/dir1/dir2

Then, /home/john/dir1/dir2 would attach itself at the end of what is already in the variable. So, the new variable would be:


Notice that in the previous line of code above, the part of the value that is already there is separated from the new part with a colon, which is ‘:’. $PATH in the line of code, expands (is replaced) by the value of PATH already in memory.

Now, the same command can be in more than one directory. When an executable command is typed without the preceding path at the prompt, the shell starts searching the value (string) of the PATH variable in memory, from left to right. So, with the above line of code, /home/john/dir1/dir2 would arrive at last, in the search. Who wants his directory to arrive last? – Nobody. So, a better way to include (add) the directory, is as follows:

$ export PATH=/home/john/dir1/dir2:$PATH

/home/john/dir1/dir2 is now at the beginning, separated from the next directory, with a colon. So, with a prompt of “[email protected]:~$”,

[email protected]:~$ export PATH=/home/john/dir1/dir2:$PATH

after typing and pressing Enter, the command, command.exe in the directory, dir2, would be executed with:

[email protected]:~$ command.exe

As long as the computer is not shut down, the user will continue to execute command.exe without typing the preceding path.

When the computer is shut down, everything in random-access-memory is erased. The bad news is that the computer user will have to repeat this process of adding his directory to the PATH variable every time the computer is rebooted. Nobody wants to be doing that today. So, the Permanent Addition approach is the recommended approach. That is, the addition to PATH, has to be saved (into the hard disk).

Permanent Addition

From the above discussion, the idea is to save the change made (into the hard disk). So, if the change is saved into a file that shell reads each time the computer boots, that would be fine. That is, each time the computer boots, the PATH variable in memory will be updated appropriately. When a computer starts, at booting, it reads certain files. One of the files the Bash shell for Linux reads is, ~/.bashrc . The file name is .bashrc , beginning with the dot. It is in the user’s directory.

Bash is just one shell, probably the most popular shell for Linux, today. Another shell for Linux is Zsh. With Zsh, the corresponding file is ~/.zshrc, still in the user’s directory. When the shell starts, at booting, it reads this file. For Bash, the file is, ~/.bashrc . So, if the above addition code is typed into the ~/.bashrc file, the directory will always be in memory, as ~/.bashrc will always include it in PATH, each time the computer boots. It remains in memory, until the computer is shut down.

In Bash, ~/.bashrc is a hidden file, so the ordinary use of the command, ls will not show it. ~/.bashrc contains some Bash (shell) commands. Ubuntu is a variant of Linux. Ubuntu comes with a text editor. In the author’s computer, the background of the Ubuntu text editor is black.

The above addition command (without the prompt) should be included to the ~/.bashrc file. It is good to add it at the end of the file in order not to temper with its available content, that is probably working alright.

To open ~/.bashrc in Ubuntu, type the following at the command prompt at the terminal, and press Enter:

$ nano ~/.bashrc

Where nano is the text editor. Here, nano is a command (executable), whose argument is, ~/.bashrc . The content of the file should open in the text editor to overlay the terminal window.

The ~/.bashrc file will open in the text editor, probably with a black background. The cursor will be flashing at the top-left corner.

Press the down-arrow key on the keyboard continuously until the cursor reaches the end of the file. Add a new line of,

$ export PATH=/home/john/dir1/dir2:$PATH

That is not all. The change to the ~/.bashrc file has not been saved. To save the file with the change made, press Ctrl+o, and follow any other instructions that appear. To exit the text editor, press Ctrl+x, and the normal terminal window should replace the text editor window. Other shell commands can then be issued.

If “echo $PATH” is issued, the new directory will not be seen in the display. There are two ways to continue from here: either reboot the computer, or get the new contents of the ~/.bashrc file into memory with the source command. It is simple to use the source command, as follows:

$ source ~/.bashrc

If the source command is issued without rebooting, then “echo $PATH” will show the new directory added in the result (display). The command of interest can then be typed without the preceding path.

Note: Do not confuse between the end of the PATH value (list) and the end of the ~/.bashrc file. Also, do not confuse between a directory in PATH in memory, and an executable file in the directory in the hard disk.


PATH is a built-in variable in Linux shells. The value of PATH is a list of directories separated by colons. There is a command (executable file) in the hard disk for each of these directories. If a command is issued at the sell without preceding it with a path, the shell will look into these directories for the command. If it does not see the command in any of the directories, then the command will not be executed. In this case, in order to execute the command, the command has to be preceded with its path. The command can be in more than one directory in the PATH value. As soon as the shell sees the first directory having the command, it executes the command. The PATH value is actually a string consisting of directories separated by colons.

The problem is how to add the directory for the executable of a new application into PATH. In some cases, this is done automatically by the installing program of the new application. In other cases, it has to be done manually. There are two ways of doing it manually, referred to as: Temporary Addition and Permanent Addition. Temporary Addition uses the export command and just adds the directory while the computer is on. Permanent Addition has to save the export command line into the ~/.bashrc initialization file (Bash) for future operations.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at I have been working in these fields for more than 10 years.