Ever wished that the apps on your Linux machine could understand where to find new commands and programs, making it easier to run them from anywhere?
Well, good news – they can!
Meet the PATH variable, your system’s index of programs.
In this comprehensive introduction, we’ll discuss the PATH variable, and show you how to view the directories in the PATH variable. We’ll round off with a short overview of how to remove a directory from the PATH variable.
Table Of Contents
- What is the PATH Variable in Linux?
- How to Work With the PATH Variable in Linux?
- How to View PATH Directories
- How to Add a Directory to the PATH Variable in Linux
- How to Remove a Directory from the PATH Variable in Linux
Let’s start with a quick introduction to the PATH variable in Linux.
What is the PATH Variable in Linux?
In Linux, the PATH variable is like a special list that tells the system where to find the necessary utilities to run commands. It’s part of the environment variables, which manage and control how things run on the system.
Imagine it as a roadmap that the system uses to locate application paths. When you use the terminal to instruct the system to perform a task, it checks the PATH variable to find out the program you’ve asked for. So, if you have installed a new application and want to use it, simply add the directory to the PATH variable.
How to Work With the PATH Variable in Linux?
Now that you have a basic understanding of the PATH variable, let’s see how to work with this critical system component.
Before trying out the following PATH variable manipulation commands, be sure that you:
- A user account with sudo or administrator privileges
- Have a working knowledge of the Linux command line
- Access to a terminal
- Have a text editor ready to use
How to View PATH Directories
Follow these steps to view the directories in the PATH variable:
Launch the terminal and run the following command:
This command will display the PATH variable as a list of directories separated by colons (:). This is the list of directories that your Linux system checks when you hit Enter on a command.
Alternatively, you can use the printenv command in the terminal to print the PATH variable:
As you can see, the PATH variable can contain several directories. If you’re looking for a specific directory, we recommend the which command:
How to Add a Directory to the PATH Variable in Linux
Whether you want it just for the current session or permanently, adding a directory to the Linux PATH variable is easy.
Temporarily Add a Directory to PATH
Launch the terminal and enter the following command, replacing /path/to/directory with the actual path:
Note that this addition will remain active until the duration of the terminal session.
Permanently Add a Directory to PATH
Adding a directory to the PATH variable in Linux on a permanent basis requires editing the .bashrc file that manages the settings for the BASH shell.
For this, open the .bashrc file in your favorite editor. For this demonstration, we’re using the nano editor.
Launch the terminal and run the following command to open the nano editor:
Scroll to the end of the file and add the line:
Save the file and exit the editor.
Note that the changes you made are not in effect. You need to reload the new settings by running the following command:
Alternatively, restart the terminal.
We recommend checking the PATH variable to see if it contains the new directory. You can use the echo command for printing the contents of the PATH variable in the terminal.
How to Remove a Directory from the PATH Variable in Linux
We’ll now discuss the two scenarios for removing a directory from the PATH variable.
Temporarily Remove a Directory from the PATH Variable
Launch the terminal and use the echo command to view the current contents of the PATH variable:
To remove a directory from PATH, use the string replacement operation:
Permanently Remove a Directory from the PATH Variable
To permanently remove a directory from the PATH, you need to edit the terminal configuration file in your favorite editor. We are using the nano editor.
Run the following command in the terminal:
Find the path you wish to remove and delete it. Save the file and exit the editor.
Now, reload the new terminal settings with the following command:
Alternatively, restart the terminal to reload the new settings.
One-Liner Technique Removal
For a quick fix, you can use the following one-liner syntax:
export PATH="$(echo $PATH | tr : '\n' | grep -v Dir1 | paste -s -d:)"
Mastering the process of adding directories to your Linux PATH, temporarily or permanently, is crucial for personal projects or server management. It significantly impacts how you interact with your system.
By customizing the PATH variable, you’re essentially telling Linux where to find and run your programs. Whether on your Linux machine or with a provider like RedSwitches, the process remains straightforward, enhancing your Linux experience for a more efficient workflow.
Q. What is the PATH variable in Linux?
The PATH variable in Linux is like a guide telling the system where to find programs. When you type a command, the system looks in the directories listed in PATH to locate the executable file.
Q. How do I view the current PATH variable in Linux?
To view the current PATH variable in Linux, open a terminal window and type
echo $PATH This will display the directories separated by a colon.
Q. How to temporarily add a directory to the PATH variable in Linux?
To temporarily add a directory to the PATH in Linux, open the terminal and use the command:
This extends the PATH for the current session, allowing easy access to executable files in the specified directory.
Q. How to permanently add a directory to the PATH variable in Linux?
To permanently add a directory to the PATH in Linux, open a terminal and edit your shell configuration file, such as ~/.bashrc or ~/.bash_profile. Add the following line:
Save the file and restart your terminal or execute source ~/.bashrc to apply the changes globally.
Q. How do I set the path in Linux without specifying the full path?
To set the path in Linux without specifying the full path every time, navigate to the directory containing the executable using ‘cd.’ Once there, use a relative path to the executable (e.g., ‘./your_executable’).
For temporary convenience, you can add the current directory to the PATH with
export PATH=$PATH:. for the current session. However, permanently adding the current directory to the PATH is generally discouraged for security reasons.
Q. How do I specify the order in which directories are searched in the PATH variable?
To determine the order in which directories are searched in the PATH variable on Linux, think of it like a to-do list. The directories listed first get checked first. If you want a specific directory checked first, put it at the beginning. If you want it checked later, add it towards the end.
Q. How do I execute a program in a directory not in the PATH variable?
To run a program in a directory not in the PATH on Linux, you can use the ./ prefix followed by the program’s name and the path to the directory. For example, if your program is in a directory called “my_program” within your current location, type ./my_program in the terminal to execute it. This way, you directly specify the program’s location without relying on the PATH variable.
Q. How do I permanently add a directory to the PATH variable in Ubuntu?
To permanently add a directory to the PATH variable in Ubuntu, you can edit the ~/.bashrc file. Open the file in a text editor and add the following line at the bottom:
export PATH=$PATH:/path/to/directory Save the file and exit. The directory will be added to the PATH variable every time you open a new terminal window in Ubuntu.