What is $ PATH in Linux and how does it work?
When you type a command in a terminal window and press Enter, it starts a lot of activity before the command runs.
Bash is the default shell on most Linux distributions. Interpret the entered text line and identify the names of the commands mixed with the parameters, pipeline, forwards and everything else is there. Then locate the executable binaries for those commands and launch them with the parameters you provided.
The first step the shell takes to locate the executable is to identify if a binary is involved. If the command you are using is inside the shell (a «embedded shell» ), no further search is required.
The integrated housing elements are the easiest to find, as they are an integral part of the housing. It’s like having a tool belt – I’m always with you.
However, if you need any of their other tools, you will need to search the store to find them. Is it on your workbench or on a wall hanger? This is $ PATHenvironmental variable. Contains a list of shell places searched and the order in which they will be searched.
If you want to see if an order is an order shell interior, an alias, a function or an independent binary mv / work / unfile, you can use typeorder as shown below:
This tells us that clearis a binary file, and the first path is in / usr / bin. You may have multiple versions of clearinstalled on your computer, but this is the one the shell will try to use.
As expected, CDit is a built-in housing.
Listing your $ PATH
It’s easy to see what’s in your way. Just type the following to use threwcommand and print the value included in $ PATHvariable:
echo $ PATH
The result is a list of :file system locations delimited by two points ( ). The shell searches from left to right through the path, checking the location of each file system to find a suitable executable to run its command.
We can choose our path through the list to see the locations of the file system that will be searched and the order in which they will be searched:
- / usr / local / sbin
- / usr / local / bin
- / usr / sbin
- / usr / bin
- / sbin
- / cos
- / usr / games
- / usr / local / games
- / snap / bin
Something that may not be immediately obvious is that the search does not start in the current working directory. Rather, it works through listed directories and only listed directories.
If the current working directory is not in your path, it will not be searched. Also, if you have commands stored in directories that are not in the way, the shell will not find them.
To prove this, we created a small program called f. When you run, fprints the name of the directory from which it was started in the terminal window. It is located in / usr / local / bin. We also have a newer version in / dave / workdirector.
We write the following whichcommand to show us which version of our program find and use the shell:
The shell reports that the version it found is the one in the directory on the way.
We write the following to activate it:
Version 1.0 a fit works and confirms that our expectations were right. The version found and executed is in / usr / local / bin.
To run any other version of f on this computer, we will need to use the path to the command line on the command line, as shown below:
Now that I’ve told the shell where to find the version fwe want to run, we use version 1.1. If we prefer this version, we can copy it to / usr / local / bindirectory and overwrite the old.
Suppose we develop a new version of f. We will have to run it frequently as we develop and test it, but we do not want to copy a new development construction in the live environment.
Or maybe I downloaded a new version f and we want to do some verification tests before they are available to the public.
If we add our working directory to the plot, we’ll make it on the shell to find our version. And this change will only affect us; others will still use the version of finto the / usr / local / bin.
Add a directory to $ PATH
You can use exportordered to add a directory to $ PATH. The directory is then included in the list of file system locations that the shell searches for. When the shell finds a suitable executable, it stops searching, so you need to make sure it looks in your directory first, before / usr / local / bin.
This is easy to do. For our example, we write the following to add our directory to the beginning of the path so that it is the first location we are looking for:
export PATH = / home / dave / work: $ PATH
This command is set $ PATHto be equal to the directory we add / home / dave / work, and then to the entire current route.
The first PATHit has no dollar sign $). We set the value for PATH. The final $ PATHhas a dollar sign because we refer to the content stored in PATHvariable. Also note the colon ( :) between the new director and $ PATHname variable.
Let’s see what the road looks like now:
echo $ PATH
Our / home / dave / workthe directory is added at the beginning of the path. The two points we offer separate you from the rest of the way.
We write the following to check if our version of fis the first we find:
The pudding test is carried out f, as shown in the following:
Shell finds version 1.1 and runs it from / home / dave / work.
To add our directory to the end of the path, we simply move it to the end of the command, as follows:
export PATH = $ PATH: / home / dave / work
Make permanent changes
What the He said Beth Brooke-Marciniak«Success is okay, but success is fleeting.» The moment you close the terminal window, all the changes you made in $ PATH . To make them permanent, you need to put them on exportcommand in a configuration file.
When you place exportorder in .bashrcfile, sets the path each time you open a terminal window. In contrast to the SSHsession, for which you need to log in, are called «interactive» sessions.
In the past, I would put exportorder in .profilefile to set the connection path to the terminal sessions.
However, we find that if we place exportorder in FILE .bashrcor .profile, set the path for interactive sessions and connection to the terminal correctly. Your experience may be different. To deal with all the eventualities, we will show you how to do it on both files.
Use the following command in /The homedirectory to edit the file .bashrcfile:
gediteditor himself open up with him .bashrcthe uploaded file.
Scroll down to the bottom of the file, and then add the following export command we used earlier:
export PATH = / home / dave / work: $ PATH
Save the file. Then close and reopen the terminal window or use pointcommand to read .bashrcfile, as follows:
Then write the following threw command to check the path:
echo $ PATH
This adds / home / dave / workdirector at the beginning of the path.
The process of adding the command to .profilethe file is the same. Enter the following command:
geditthe editor starts with .profilethe uploaded file.
add exportcommand at the end of the file and then save it. Closing and opening a new terminal window is not enough to force .profileREADING of file. For the new settings to take effect, you need to log out and sign back in or use pointorder as shown below: