How to set $PATH variable in Linux

Ubuntu PATH Variable

The $PATH variable is an essential part of Linux and other Unix-like operating systems. It contains a list of folders with different executables on the system and advises the shell where to search for these programs.

On a new Linux establishment, the $PATH variable already contains the common locations where programs are stored, like /bin, /usr/bin, and /usr/local/bin/, so that most programs work out of the box. Nonetheless, sometimes you like to run custom scripts or projects and want to add them to the system path variable.

In such cases, you want to set the $PATH variable properly before running those projects.

$PATH in Linux

When the Linux shell gets a command or an exec call from a program, it refers to the $PATH environment variable, which is a colon-separated list of folders. The /bin, /usr/bin, and /usr/local/bin folders are commonly included in the $PATH variable, with superuser receiving two additional entries, /sbin and /usr/sbin. Nonetheless, you can add new sections to your $PATH variable to advantageously send off contents and projects from the terminal regardless of the current working index.

Why set the $PATH variable

Many programs on Linux can be run without specifying their exact location or path. E.g. when you run the cd command, the shell executes it without error and without needing you to specify the command’s entire path.

When you need to run a custom script, you can’t use the exec command the same way you did with the cd command. Instead, go to the script’s directory (through terminal) and run the command from there, or specify the script’s absolute path in the command to help the shell find it if you want to run it from anywhere on the system.

While both strategies are effective, they are inefficient and can rapidly turn the procedure into a tedious ritual. Adding their source directory to your shell’s $PATH so that it knows where to find the script and can execute it swiftly is an alternate (read: efficient and effective) way to launch programs from non-standard folders.

Setting up $PATH variable in Linux

There are many ways to add a directory to your $PATH, depending on what the user wants to set, whether it’s a one-time setup to run a script or a permanent configuration to use a script regularly.

A temporary way to set $PATH

To set a $PATH variable temporarily, use the following syntax to add directories to the variable in the terminal:

Here, the default path is given below.

user@osnote:~$ echo $PATH

PATH Variable in Linux

Now, the syntax to add directories

The syntax:

user@osnote:~$ export PATH=$PATH:/new/additional/path

Example:

user@osnote:~$ export PATH=$PATH:/home/administrator/scripts

Add folder to PATH

Here, the path is given /home/user/scripts to set the variable temporarily, and the variable is set as shown in the figure. These changes are only for the active session. When the system is restarted, the default setting is reset.

(Note: user and scripts are the directories.)

Permanent way to set $PATH

Assuming there’s a program on your framework that you’ll probably utilize often, you should permanently set the $PATH variable. This process mentioned below will guarantee that the shell remembers its directory even after the system is rebooted. To set the $PATH permanently, we should edit the shell configuration file and set the $PATH variable.

To identify the shell of the system you are running, execute the command below:

user@osnote:~$ echo $0

After knowing the shell, we need to edit the configuration file. In my case, the shell is a bash shell, so the bashrc file is being edited.

user@osnote:~$ vim ~/.bashrc

Edit BASHRC file

Here, the path is set to permanent after editing the configuration file.

user@osnote:~$ echo $PATH

New PATH has been set

Conclusion

The above-given command and its example are to set $PATH variable in Linux on Ubuntu 22.04. Thank you for checking it out!

Published
Categorized as Linux