Skip to Content

What is PATH variable in Linux and how it works

The PATH variable is a list of directories that contains executable programs. It controls where your shell will look for commands on your system.

When you enter a command in the terminal, such as grep, ls, or echo, the shell looks for the command in the directories listed in the PATH variable. If it finds the command in one of these directories, it will execute it.

Before we dive into the details of the PATH variable, let us take a look at what is an environment variable.

What is environment variable in Linux

I first came across them while working on a project as a Linux sysadmin. At first, I didn’t fully understand their purpose and it was difficult for me to understand why they were necessary.

After doing some research and trial-and-error experimentation, I began to better grasp the concepts surrounding environment variables.

Environment variables are process-wide variables built into your system and interface that control the way your system looks, acts, and “feels” to the user, and they are inherited by any child shells or processes.

Environment variables in Linux are system-specific, named object which stores specific information related to the current user’s session, such as a username or home directory path.

These variables can be configured and modified to suit different users and systems.

Environment variables are always uppercase, as in HOME, PATH, SHELL, and so on. These are the default environment variables that come on your system. A user can also create their own variables.

You can view all your default environment variables by entering env into your terminal from any directory.

env

check the value of PATH variable in Linux

The PATH variable is usually set by default to a list of directories that are commonly used to store executables, such as /usr/bin and /usr/local/bin.

You can view the current value of the PATH variable by typing the following command in the terminal:

echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

These are the directories where your terminal will search for any command. When you enter ls, for example, the system knows to look in each of these directories for the ls command, and when it finds ls, the system executes it.

Each directory is separated by a colon (:). Don’t forget to add the $ content symbol to PATH. When we put a $ before a variable, we are asking the system for the content of the variable.

Now let’s see what echo command does.

The echo command is one of the most basic and useful commands in Linux. It’s used to output a string of text to the terminal window, or to other locations such as files, devices, and other programs.

The syntax for using the echo command is very simple:

echo [string]

Where [string] is the text you want to output. For example, this command will output the text “Hello World!”:

echo "Hello World! "

The echo command can also be used to display environment variables like we did in this example.

command not found error in Linux

Most commands are located in the sbin or bin subdirectory, like /usr/local/sbin or /usr/local/bin.

If a directory is not present in the PATH environment variable, you may receive an error message saying “command not found“, or “error: command not recognized“.

This error can indicate that the command attempted is not present in any of the directories included in the PATH variable.

In this case, it is necessary to add the relevant directory path to the PATH variable in order to make the command available.

add directory to the PATH variable in Linux

Now let’s see how to change a variable’s value.

You can add your own directories to the PATH variable by modifying the PATH variable.

For example, if you are using the bash shell, you can add a directory to the PATH variable:

export PATH=$PATH:/path/to/directory

This will append /path/to/directory to the end of the PATH variable, so the shell will search this directory for executables when you enter a command in the terminal.

This will set the environment variable for the current session, but it will be lost when you close your terminal session. To make it permanent, you need to add it to your system configuration files.

If you are using bash, you can add PATH variable to file .bash_profile.

It is a hidden configuration file in your home directory. To open it for editing, enter the following command:

vi ~/.bash_profile

This will open the .bash_profile file in the vi editor. You can then modify your PATH variable. Once you are done, press CTRL+X to save and exit.

export PATH=$PATH:/path/to/directory

This will make the environment variable permanent, and it will be set every time you open a new terminal session.

delete a directory from PATH variable in Linux

To delete a directory from the PATH environment variable in Linux or other Unix-like operating systems, you can use the sed command to remove the directory from the PATH variable.

For example, suppose you want to delete the directory /usr/local/bin from the PATH variable.

First, you need to view the current value of the PATH variable like we discussed above by typing the following command in the terminal:

echo $PATH

This will display the directories that are currently in the PATH variable, separated by colons.

Next, you can use the sed command to delete the directory from the PATH variable. The sed command is a utility that allows you to edit and manipulate text strings.

To delete a directory from the PATH variable, you can use the s/old//g command, which searches for the old string and remove it.

For example, if you need to delete the /usr/local/bin directory from the PATH variable, you can use the following command:

export PATH=$(echo $PATH | sed 's/:\/usr\/local\/bin//g' |sed 's/^://')

This will remove the /usr/local/bin directory from the PATH variable, but it will only be temporary. To make the change permanent, you still need to add the export command to your shell’s configuration file.

You can also assign the new list of directories to PATH variable without using sed command.

export PATH=/usr/bin:/usr/local/sbin:/usr/sbin:/home/ocp/bin

understanding env variables in Linux

Environment variables are stored in a shell’s environment, which is a collection of variables that can be accessed by the shell and all the sub shell launched by the current shell.

Environment variables can be set in a number of ways, including by the shell itself, by the system administrator, or by the user.

They can be set temporarily for a single command or permanently for a user or system-wide.

Environment variables can be used to store a wide variety of information, such as the directories that a shell should search for executables, the default editor to use, or the language and character encoding to use for displaying text.

To view the current environment variables in a terminal, you can use the printenv command.

To set an environment variable, you can use the export command followed by the name of the variable and the value you want to assign to it. For example, to set the EDITOR environment variable to the vi editor, you can use the following command:

export EDITOR=vi

Common ENV Variables

HOME – The user’s main working directory is the default directory when the user logs in to the Linux system.

$ whoami
howtouselinux
$ echo $HOME
/home/howtouselinux

HISTSIZE – Save the number of historical commands. The commands we input will be saved by the system, and this environment variable records the number of commands to be kept. Generally 1000.

$ echo $HISTSIZE
1000
$ HISTSIZE=1001
$ echo $HISTSIZE
1001

 

Conclusion

The PATH variable is an important operating system environment variable. It tells the system which directories to search in order to find a particular program or command.

When a command is issued, the operating system will scan through all the directories specified in the PATH variable for an executable file of that command.

If it does not find one, it will return an error message indicating that the command was not found.

By adding relevant directory paths to the PATH variable, users can make programs and commands available to be executed on their Linux system.

I hope this helps! Let me know if you have any questions.