Linux add path to path environment variable


Linux add path to path environment variable

1、 Path environment variable

The path variable is the place where the Linux system stores the search path of the executable file, which is equivalent to the path variable of the windows system. Because I haven’t used it in other operating systems, I don’t know the details.

Similar to the system variables and user variables of windows system, there are two types of environment variables in Linux

  • Variables created or modified by various configuration files loaded at system startup, such as / etc / profile and all sh files in the / etc / profile. D folder loaded at the end of it. They are similar to Windows system variables and are visible to all users.
  • Variables created or modified by various configuration files loaded by the user when logging in, such as the configuration file of the command interpreter (shell) used by the user. Their windows like user variables are only visible to the current user.

Because not all the applications in Linux are installed globally, or they may be installed separately for users, there are two kinds of paths in the path variable: system level and user level.

2、 Add method

1. Use the export statement directly

For example:

# /etc/profile.d/
export PATH=$PATH:/usr/local/jvm/bin


export PATH=$PATH:~/.local/bin

The biggest drawback of this method is that it is easy to add repeatedly, especially when the statements written in the configuration file of the user’s shell are repeatedly inserted when the user reloads the shell configuration file. Of course, the duplication caused by reloading the configuration file can be basically solved by writing in the shell configuration file loaded only during the login period (such as zshell’s. Zlogin).

2. With the help of function

In the / etc / profile file of archlinux, I found a magic function and wrote it in my. Zshrc

    case ":$PATH:" in
appendpath '~/.local/bin'
unset -f appendpath
export PATH

Before adding a path to the path variable, the case matching mechanism is used to determine whether the path to be added already exists in the path variable, so as to determine whether to add it. After the execution, the use command is used to contact the occupation of the function name.

This is also the method I use at present, which can perfectly avoid the problem of path duplication. Although it is only used to modify path, it can also be used for list type environment variables like classpath.

End note: my main purpose of blogging in blog Garden is to find a convenient cloud note taking platform, which is all usual notes. Individuals do not pursue rigorous writing. So please make complaints about the blog users.

Recommended Today

Rust and python: why rust can replace Python

In this guide, we compare the rust and python programming languages. We will discuss the applicable use cases in each case, review the advantages and disadvantages of using rust and python, and explain why rust might replace python. I will introduce the following: What is rust? What is Python? When to use rust When to […]