Linux Commands

How to change the path in the Linux terminal?

When you input a command into your Linux terminal, it does not search each directory to check any program with the same name. It only looks into the specified directories. The question that should come into your mind is how the terminal knows to look in the directories mentioned above? The answer to this question is straightforward. These directories are part of the $PATH environment variable, which the terminal uses to determine where to search. Any beginner Linux user should know how to change $PATH in the Linux terminal.

Viewing PATH

Suppose you want to install programs in a different location on your system and run the program without mentioning their location. A simple way to accomplish this is to change your $PATH variable. Write out the following command to view what exists currently in your $PATH:

$ echo $PATH

Most likely, you will see the various directories separated by colons. Now let’s check out the methods of changing the $PATH in the Linux terminal.

Method 1: Temporarily changing $PATH in terminal

For instance, you have a directory named “bin” in your Home directory, and you want this location to be where you save all of your shell scripts. You can easily make these changes by modifying the $PATH variable. The modified $PATH variable will be exported to the shell child process environments using the “export” command. In addition, after modifying the $PATH, you can now execute any shell script by only specifying the executable script name rather than typing the entire path of the file. These settings will change the $PATH, but temporarily.

$ export PATH="$HOME/bin:$PATH"

echo” the $PATH variable to confirm the changes we made into the path of the Linux terminal.

$ echo $PATH

Method 2: Permanently changing $PATH in terminal

To change the $PATH permanently, you must define the $PATH variable configuration files. When you start a new session in most Linux versions, environment variables are read from the shell configuration files customized for each user. If you are a bash user, utilize the  “~./bashrc” file for changing $PATH. First, open the “~./bashrc” file in the text editor by typing the following command.

$ nano ~/.bashrc

Write out the below-given lines into this configuration file.

export PATH="$HOME/bin:$PATH"

Execute the source command to save the file with the change we made into the $PATH. This command will also load the new path in the current session of the Linux terminal.

$ source ~/.bashrc

Again, print out the value of $PATH for the confirmation of the changes.

$ echo $PATH

The output declares that the $PATH is successfully changed in the Linux terminal.

Conclusion

Changing  $PATH in the terminal comprises a simple yet straightforward procedure. This allows any Linux user to run scripts and commands from a non-standard location without specifying the entire path of those executable shell scripts. This article has provided you two different methods for changing $PATH in the Linux terminal.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.