Home > System Tutorial > LINUX > body text

What files do Linux environment variables come from?

王林
Release: 2024-03-20 19:50:10
forward
660 people have browsed it

Linux environment

After the LinuxShell login is successful, Linux will obtain a series of data from the file for the login. This data will be used in individual instructions or individual programs. That data is called the LinuxShell runtime environment. Data in the environment can be roughly divided into four types: environment variables, Shell variables, aliases, and Shell functions. Among them, Shell variables, aliases, and Shell functions will not be explained in detail here.

What are the environment variables?

You can directly use the printenv command without parameters to output the environment variables of the current session and the values ​​of the environment variables. If parameters are added, the value of a certain variable is output. If it is more convenient to view, you can pass the output of printenv to less to view the environment variables (1):

printenv | less
Copy after login

The following is part of the output on my Linux system:

...
...
MANDATORY_PATH=/usr/share/gconf/cinnamon.mandatory.path
XDG_SESSION_ID=c2
XDG_GREETER_DATA_DIR=/var/lib/lightdm-data/rit
USER=rit
DESKTOP_SESSION=cinnamon
QT4_IM_MODULE=fcitx
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/e9e1def3_9380_43b6_8ce3_7916861e45d2
DEFAULTS_PATH=/usr/share/gconf/cinnamon.default.path
QT_QPA_PLATFORMTHEME=qt5ct
PWD=/home/rit
HOME=/home/rit
...
...
Copy after login

As you can see, PWD, HOME, USER, etc. that you usually come into contact with are all among them.

If you want to use the value of a variable in the parameters of the command, you can enter "$ variable name" (2). Such as:

ls $HOME/bin
Copy after login

User-defined variables

Not only the variables that come with the system, users can also customize variables:

rit@rit-X405UA:~$ foo=FOO
rit@rit-X405UA:~$ hello='Hello World'
rit@rit-X405UA:~$ echo $foo
FOO
rit@rit-X405UA:~$ echo $hello
Hello World
Copy after login

Note that there are no spaces on the left and right sides of the equal sign, because spaces are regarded as separators in shell commands and are not meaningless symbols.

Note that if special characters appear, such as spaces, $, etc., they must be expanded with colons (3).

If you want the program running in the shell to access the variable, you need to use the export command:

rit@rit-X405UA:~$ export foo hello
Copy after login

Variables defined in the current session like this are only valid in the current session, that is, the variable will not exist when you log out and log in again. To use this variable every time you log in to the shell, you need to define the variable in the environment variable configuration file.

What file do environment variables come from?

As mentioned at the beginning of the article, Linux obtains environment variables from files. So what file provides environment variables for LinuxShell? (Loginshell and non-loginshell will be explained earlier)

linux修改环境变量的值_linux修改用户环境变量_修改环境变量linux

For loginshell:

/etc/profile, this file is the global environment variable configuration file of loginshell. Global means that it is valid for all users. ~/.bash_profile, ~/.bash_login, ~/.profile, these three files are used to configure users. Personal environment variables, so each user's HOME directory will have at least one of these three files (depending on the Linux distribution version) linux modifies user environment variables, only reads when reading Take one of them. When loginshell logs in, Linux will first read the global configuration file Linux memory management /etc/profile, then search for these three files in a certain order in the HOME directory, and finally read the first file found. If there is a conflict with a variable defined in /etc/profile, the variable will be overwritten.

For non-loginshell:

/etc/bash.bashrc, this file is the global environment variable configuration file of non-loginshell. (In the introduction of some blogs on the Internet, this file is not /etc/bash.bashrc but /etc/bashrc. In fact, this depends on the distribution version) ~/.bashrc, this file is the user’s personal non-loginshell environment variable configuration file. Like loginshell, this file is executed after /etc/bash.bashrc. If there is a conflict, this file will also redraw the conflicting variables.

The above description of the file reading order is referred to this article.

Various Linux distributions have a command su. If you directly "su username", you will log in to the user as a non-loginshell. If you add the option "-" or "-l" or " --login", the user will be logged in as loginshell. If readers have a clearer understanding of the reading process of such files, they can use this command to test by changing those files.

loginshell and non-loginshell

There are two ways to log in to LinuxShell: loginshell and non-loginshell. The login shell is generally used as the first login shell (for example, when the computer is turned on), while the non-login shell is generally the shell that is started directly from the GUI after the computer is turned on. There will be some differences in the environment variables used to log in through these two methods.

1.non-loginshell will inherit some environment variables from the previous process (usually loginshell)

Can be verified through a simple test.

First, customize a variable foo=FOO in the current shell and export it (in order to allow child processes to also use this variable, and the shell itself is a program):

rit@rit-X405UA:~$ foo=FOO
rit@rit-X405UA:~$ export foo
Copy after login

Next, log in to another account bob with loginshell, and try to view the foo variable:

rit@rit-X405UA:~$ su - bob
Password:
bob@rit-X405UA:~$ echo $foo
bob@rit-X405UA:~$
Copy after login

The result is that the foo variable is empty, which means that foo is not defined.

Next, log in to another account bob with non-loginshell and view the foo variable:

rit@rit-X405UA:~$ su bob
Password:
bob@rit-X405UA:/home/rit$ echo $foo
FOO
Copy after login

The result is that the foo variable is consistent with the definition in the shell of the user rit.

linux修改用户环境变量_修改环境变量linux_linux修改环境变量的值

Explains that non-loginshell inherits the user-defined variables of the previous process, but loginshell does not.

2.PWD, HOME, USER, PATH

As can be noticed in the example in 1, the PWD of loginshell is changed to the current user's HOME directory (~), while the non-loginshell inherits the working directory of the parent process.

If you use echo to view HOME and USER, we will find that after logging in with both login forms, these two variables are switched to the home directory and username corresponding to the current user.

For the difference between the two login forms of PATH, you can find some clues in ~/.bash_profile (or ~/.bash_login, ~/.profile). As mentioned above, these three files are read in loginshell. If we carefully compare them with ~/.bashrc, we will find that there are two more lines in ~/.bash_profie:

...
PATH="$HOME/bin:$PATH"
...
PATH="$HOME/.local/bin:$PATH"
...
Copy after login

So after logging in using the two methods, use echo to view the PATH variable and you can see the difference as follows:

non-loginshell:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Copy after login

linux修改环境变量的值_linux修改用户环境变量_修改环境变量linux

loginshell:

/home/rit/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr /local/games:/snap/bin
Copy after login

$HOME/bin is not included in the above because the home directory does not have this folder.

If you look carefully, you can find that there are other differences, but the source of these differences is not easy to find.

Comments

(1) Linux shell provides a feature called pipeline, which means that the output of one command can be redirected to the input of the next command (that is, the input of a certain command is regarded as the input of another command), but the prerequisite It is necessary for the instruction to accept input or form output. Common instructions include cat, less, grep, etc. Input and output are spliced ​​with "|".

(2) When linuxshell reads the $ symbol, it will first treat the characters immediately before it as a variable name, replace $ and the variable name with the value of the variable, and then replace the replaced Parameters are passed to the command. This belongs to the parameterexpansion in the shell's characteristic expansion, not only parameterexpansion, but also pathnameexpansion, braceexpansion, and commandsubstitution.

(3) is the opposite of (2). If you want to ignore special symbols, you can add a dash on the right. Double colon will block all special symbols except "", "`", and "$". A single colon ignores all special characters.

refer to

Not only the order of reading environment variable files is referred to the website's blog Linux Modification of User Environment Variables, but other contents are also referred to from the book: TheLinuxCommandLine2ndEdititonACompleteIntroduction.

Errata calibration July 4, 2019: (1) When customizing variables, no spaces can be left on the left and right sides of the equal sign. Before the change, I only mentioned that there should be no space left to the right of the equal sign. (2) parameterexpansion will replace "$variable name" with the value of the variable. The description before the change is to convert "$variable name" into a variable name. (3) escapecharacter does not belong to expansion characteristics. Before the change, I classified it into expansion. New content July 4, 2019: Added a description of the scope of custom variables at the end of the "User-defined variables" section.

The above is the detailed content of What files do Linux environment variables come from?. For more information, please follow other related articles on the PHP Chinese website!

source:itcool.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!