
Have you ever wondered how Linux and other Unix-like operating systems know where programs are located when you try and run a command? The path variable is responsible for this, it tells the operating system where to search for executable files. Contained in the path variable is a list of directories that are searched though in order till the file with the same name is found and run.
In this post we will explore related background, how to check your path, how the path works, and how to add more file paths to your path.
What Is A Variable?
A variable let’s take something like $file to represent the path to a file. User variables can be set by scrips or from the command prompt directly.
What Are Environmental Variables?
Unlike shell variables, environment variables are system variables related to the environment you’re working from. Here are some examples of system environment variables:
- PATH: Specifies the directories where the system looks for executable files.
- HOME: Points to the current user’s home directory.
- USER: Contains the username of the current user.
- SHELL: Specifies the default shell for the user.
- PWD: Represents the present working directory.
- LANG: Sets the default system language.
- EDITOR: Defines your preferred text editor
To Check An Environment Variable.
To check one environment variable you can use the following syntax:
echo $VARIABLE
To see all of them run one of the following commands:
printenv
env
What is the PATH variable?
The PATH variable contains the PATHs that the system for look for commands/executable files. So when you run a command for example the ls command, it finds an executable file at /usr/bin/ls and then runs it.
Understanding The Importance Of Order Within PATH
Order matters when it comes to path, say your path points to these three locations (I expect your path will be longer)
/usr/local/bin:/usr/local/sbin:/usr/bin
The system will check the first location and do one of two things.
- Run the file named the command
- Or move on to the next location
It will loop through this until a command is run or there are no places left to check, if that happens you will get a command not found.
Managing PATH
The total path will be put together from a few different locations, ones managed by the system administrator and others a user can edit.
Files the system administrator manage
- /etc/environment: This file can be used to set system-wide environment variables like PATH.
- /etc/profile: This file is executed for login shells and can set system-wide environment variables.
- /etc/profile.d/: This directory contains scripts that are sourced by /etc/profile and can be used to set additional environment variables, including PATH.
- /etc/bash.bashrc or /etc/bashrc: These files are system-wide bashrc files and can also be used to set system-wide PATH variables.
Files users can modify there path from
- ~/.bash_profile, ~/.bash_login, or ~/.profile: These files are specific to individual users and are executed for login shells. Users can set their own PATH variable in these files.
- ~/.bashrc: This file is executed for interactive non-login shells and can be used by users to set their own PATH variable.
Adding To PATH
When editing the path variable it’s important to append it to the existing path to avoid users no longer being able to run applications.
This can be done by adding the following line to the desired file from above, let’s use ~/.bashrc.
Open the file in your preferred text editor.
nano ~/.bashrc
Add the following line.
# Set path
export PATH=$PATH:~/.local/bin
To apply this you will either need to open a new terminal or run the following command.
source ~/.bashrc
This example adds the path ~/.local/bin which is Python, node, and ruby applications get installed when run by a user among other things.
Conclusion
In this blog post, we have discussed how the Linux path variable contains file paths to directories that contain the executable files that are run when running commands. Meaning if you would like to run an executable file without the full path you will want a make sure the location is listed in the path. This applies to custom scripts, installed Python applications, etc. Be sure to keep in mind that order matters, and you should be good to go.

Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.

Leave a Reply