Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the steps to add a directory to $PATH in Linux

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The content of this article mainly revolves around the steps of adding a directory to $PATH in Linux. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!

Have you ever wondered why ls commands can be found in any directory in Linux systems? This has a lot to do with $PATH. $PATH is one of the silent manipulators in the background of Linux computers. It will quietly affect your user experience, but there is nothing dark about it. We will explain what it does and how to adjust it.

What is $PATH on Linux and how does it work? When you type a command in a terminal window and press Enter, you start a lot of activity even before the command is executed.

Bash is the default Shell in most Linux distributions. It interprets the lines of text you enter and identifies the names of commands that are mixed with parameters, pipes, redirects, and anything else. Then, locate the executable binaries for these commands and start them with the parameters you provide.

The first step taken by Shell to locate an executable is to determine whether binaries are included. If the command you use is in the shell itself ("shell built-in"), no further search is required.

Shell built-in programs are the easiest to find because they are indispensable to Shell. It's like putting them in a tool belt-they're always with you.

However, if you need one of the other tools, you must rummage through the workshop to find it. Is it on the workbench or hanging on the wall? This is what the $PATH environment variable does. It contains a list of locations for shell searches and the order in which they are searched.

If you want to see if the command is a built-in Shell, alias, function, or stand-alone binary mv / work / unfile, you can use the type command, as follows:

Type clear type cd

This tells us that clear is a binary file and that the first file found in the path is located in / usr/bin. You may have multiple versions of clear installed on your computer, but this is the version that the shell will try to use.

Not surprisingly, cd is a built-in shell.

# # list your PATH)

It's easy to see your path. Simply type the following command to use the echo command and print the value saved in the $PATH variable:

Echo $PATH

The output is the location in the file system separated by the colon's list (:). Shell searches from left to right by path to check that each file system location has a matching executable to execute the command.

We can select the method through the list to see the location of the file system to be searched and the search order:

/ usr/local/sbin

/ usr/local/sbin

/ usr/local/bin

/ usr/local/bin

/ usr/sbin

/ usr/sbin

/ usr/bin

/ usr/bin

/ sbin

/ sbin

/ bin

/ bin

/ usr/games

/ usr/games

/ usr/local/games

/ usr/local/games

/ snap/bin

/ snap/bin

It may not be immediately obvious that the search does not start in the current working directory. Instead, it works through the listed directories (only the listed directories).

If the current working directory is not in your path, it will not be searched. Also, if you store commands in a directory that is not in the path, Shell will not find them.

To prove this, we created a Mini Program called rf. After execution, rf prints the name of the startup directory in the terminal window. It is located at / usr/local/bin. We also have a newer version in the / dave/work directory.

We type the following which command to show us which version of the program the shell will find and use:

Which rf

The shell reports that the version found is the version in the directory in the path.

Let's type the following to start it:

Rf

Rf version 1.0 runs and confirms that our expectations are correct. The version found and executed is located at / usr/local/bin.

To run any other version of rf on this computer, we must use the path to the executable file on the command line, as follows:

. / work/rf

Now that we have told Shell where to find the version of rf we want to run, it uses version 1.1. If you want to use this version, you can copy it to the / usr/local/bin directory and overwrite the old version.

Suppose we are developing a new version of rf. We need to run it frequently when developing and testing it, but we don't want to copy the unreleased development version to the real-time environment.

Or maybe we've downloaded a new version of rf and want to run some verification tests on it before making it public.

If you add the working directory to the path, let the shell find our version. And this change will only affect us, others will still use the rf version of / usr/local/bin.

Add directories to your PATH) you can use the export command to add directories to $PATH. The directory is then included in the list of file system locations searched by the shell. When the shell finds a matching executable, it stops searching, so you need to make sure you search the directory first, then / usr/local/bin.

It's easy to do. For our example, we type the following command to add the directory to the beginning of the path, so it is the first location to be searched:

Export PATH=/home/dave/work:$PATH

This command sets $PATH to equal the directory we want to add / home/dave/work, and then equals the entire current path.

The first PATH does not have a dollar sign ($). We set the value for PATH. The last $PATH has a dollar sign because we refer to what is stored in the PATH variable. Also note the colon (: new directory and between) $PATH variable name.

Let's see what the current path looks like:

Echo $PATH

Our / home/dave/work directory has been added to the beginning of the path. The colon we provide separates the rest of the path.

We type the following to verify that our version of rf is the first one found:

Which rf

The proof in the pudding is that rf is running, as follows:

Rf

Shell finds version 1.1 and executes it from / home/dave/work.

To add a directory to the end of the path, simply move it to the end of the command, as follows:

Export PATH=$PATH:/home/dave/work permanent change (Making the Changes Permanent) as Beth Brooke-Marciniak said: "success is good, but success is short-lived." After you close the terminal window, any changes made to $PATH will disappear. To make them permanent, you must put the export command in the configuration file.

When you place the export command in a .bashrc file, it sets the path each time the terminal window is opened. Unlike SSH sessions that must be logged in, these sessions are called "interactive" sessions.

In the past, you needed to put the export command in the .profile file to set the path to the terminal session.

However, we found that if you put the export command in a .bashrc or .profile file, it will correctly set the path to the interactive session and the login terminal session. Your experience may be different. To deal with all possible scenarios, we will show you how to handle it in two files.

Use the following command in the / home directory to edit the .bashrc file:

Gedit .bashrc

The gedit editor opens and loads the .bashrc file.

Scroll to the bottom of the file and add the following export command we used earlier:

Export PATH=/home/dave/work:$PATH saves the file. Next, close and reopen the terminal window, or use the dot command to read the .bashrc file, as follows:

. .bashrc. ".bashrc then, type the following echo command to check the path:"

Echo $PATH

This adds the / home/dave/work directory to the beginning of the path.

The procedure for adding commands to the .profile file is the same. Type the following command:

Gedit .profile

The gedit editor starts after the .profile file is loaded.

Add the export command to the bottom of the file and save it. Closing and opening a new terminal window is not sufficient to force the .profile file to be reread. For the new settings to take effect, you must log out and log back in or use the dot command, as follows:

. .profile # # set the road for everyone (Setting the Path for Everyone)

To set the path for everyone using the system, you can edit the / etc/profile file.

You will need to use sudo, as follows:

When the sudo gedit / etc/profilegedit editor starts, add the export command to the bottom of the file.

Save and close the file. The changes will take effect the next time someone else logs in.

Security considerations (A Note on Security) make sure that you do not accidentally add a colon ":" to the path, as shown below.

If you do so, this will first search the current directory, which poses a security risk. Suppose you download an archive file and extract it to a directory. You look at the file and see another compressed file. You call unzip again to extract the archive.

If the first file contains an executable named unzip, which is a malicious executable, you may accidentally launch the file instead of the real unzip executable. This occurs because the shell will first look in the current directory.

Therefore, be careful when typing the export command. Use echo $PATH to view them and make sure they meet your expectations.

Thank you for your reading. I believe you have some understanding of "what are the steps to add a directory to $PATH in Linux?" go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report