How To Add Directory To PATH Variable In Linux?

Linux provides different environment variables which $PATH is one of them. The $PATH environment variable is used to store multiple paths or directories that contain commands, and executables. These commands and executables can be called via the command line interface or terminal just using their name without fully providing a complete path. We can add new paths to call commands and executables into the $PATH environment variable. In this tutorial, we examine how to add a new directory into the $PATH environment variable.

Display PATH Environment Variable

We can display the current $PATH environment variable using the echo command. The echo command with the $PATH variable lists the existing directories.

$ echo $PATH
/home/ismail/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin

From the output, we can see that every directory in the $PATH environment variable is delimited with the colons.

Add Directory To PATH Environment Variable

We can add a new directory to the PATH variable using the command line interface and export command. In the following example, we add the “/home/ismail/bin” as a new directory to the current PATH environment variable. We set the PATH environment variable by adding the colon. The export command used to make the PATH environment variable by all other shells currently opened or will be later opened.

$ export PATH="$PATH:/home/ismail/bin"

Add Directory To Start of PATH Environment Variable

Generally, a new directory is added to the end of the PATH environment variable in order to preserve the current order and separate the default directories with user-added directories. We can add the directory to the start of the PATH environment variable. In the following example, we add the “/home/ismail/bin” to the start of the PATH environment variable.

$ export PATH="/home/ismail/bin:$PATH"

Use Path Shortcuts While Adding Directory To PATH

The Linux shell provides path shortcuts using environment variables. For example, $HOME refers to the current user’s home directory. We can provide a new directory by using the $HOME shortcut. In the following example, we use the $HOME variable to define the directory.

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

Add Directory To PATH Environment Variable Permanently

By default, the PATH variable update is done via the command line interface but this is a temporary definition. when the computer restarts the newly added directory is removed from the PATH. We can make the new directory in the PATH environment variable permanent. In order to make the new directory in the PATH permanent, we should add this into the configurations files like /etc/environment , /etc/profile or ~/.bashrc .

Make Permanent System Wide:

See also  Install pip3 On Ubuntu

Add the following configuration to the “/etc/environment” or “/etc/profile” file.

export PATH="$PATH:/home/ismail/bin"

Make Permanent for Current User:

Add the following configuration to the “~/.bashrc”.

export PATH="$PATH:/home/ismail/bin"

Leave a Comment