Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


LPI Linux Essentials Exam 010-160 - Topic 2.1 - PATH Variable

The PATH environment variable in Linux is used to specify a list of directories where the system looks for executable files, such as binaries, scripts or commands. It enables users to run commands and programs from anywhere in the terminal without specifying the full path to the executable file.

Viewing the PATH Variable

To view the current value of the PATH variable, you can use the echo command which is used to display/print text to screen:

Exercise: Using the echo command, display the current value of the PATH variable:

echo $PATH

This will display a colon-separated list of directories, for example:

/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Exported vs Non-Exported Variables

Before proceeding it is worth knowing the difference between exported and non-exported variables, especially as the PATH variable is typically exported.

Exported Variables

Exported variables in Linux are environment variables that have been made available to child processes by using the export command. These variables are accessible not only to the current shell session but also to any child processes or subshells spawned from it. Exported variables are essential for configuring the behavior of programs and scripts, as they provide a consistent and shared storage space for data that needs to be accessed system-wide.

Non Exported Variables

Non-exported variables, on the other hand, are limited in scope to the current shell session where they were defined. They are not automatically passed to child processes or subshells. These variables are typically used for temporary or session-specific purposes and are not accessible to other programs or scripts running in separate sessions or subshells. They offer isolation and encapsulation of data within the confines of the current shell session.

Adding a Directory to the PATH Variable

To add a directory to the PATH variable, you can do this temporarily at the command line or permanently by using system or user configuration files.

Temporary Addition

From the command line, the export command can be used to set the PATH variable and the below exercise will demonstrate this:

Exercise: Add a directory to the PATH variable:

Run the following command create a scripts directory for your user and add it to the PATH variable:

Notice how when setting the PATH variable we use PATH= but when we are referencing the variable we add a $ in front i.e. $PATH:

mkdir -p /home/user/scripts
export PATH=/home/user/scripts:$PATH

To view the updated value of the PATH variable, run:

echo $PATH

The output will now contain the scripts directory as well as the directories from the earlier exercise:

/home/user/scripts:/home/user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Permanent Addition

For a permanent addition, you need to modify one of the configuration files typically used for setting environment variables. The choice of file depends on the specific Linux distribution and the user's shell.

Commonly used files for setting the PATH variable include:

  • /etc/profile: System-wide configuration file, executed for all users during login.
  • /etc/bashrc: System-wide Bash-specific configuration file.
  • ~/.bashrc: User-specific Bash configuration file.
  • ~/.bash_profile: User-specific Bash login configuration file.
  • ~/.profile: User-specific login configuration file (used by some shells other than Bash).

Depending on the file you choose to edit, it may or may not already have the PATH variable set. As with the previous section, Temporary Addition, you can use the same method to declare the variable:

export PATH=/home/user/scripts:$PATH

When updating configuration files you either need to restart the terminal or run the appropriate command (bash -l for example) to reload the configuration file for the changes to take effect.

Running a Command

Once you have added a location to the PATH variable where an executable file exists, you can directly use the command from anywhere in the terminal without specifying its full path.

In this lessons example, /home/user/scripts was added to the PATH variable, so if a executable was created in that location called myscript.sh you would be able to run it as follows:

myscript.sh

Whereas if /home/user/scripts was not a location within the PATH you would have to run the executable like this:

/home/user/scripts/myscript.sh

Conclusion

The PATH environment variable is a important component in Linux, allowing users to execute commands and programs from anywhere in the terminal. Understanding how to view, modify, and add directories to the PATH variable is essential for efficient system navigation and command execution.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.