search
HomeSystem TutorialLINUXWhat files do Linux environment variables come from?
What files do Linux environment variables come from?Mar 20, 2024 pm 07:50 PM
linuxenvironment variablesred hat linux

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

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
...
...

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

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

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

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

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:~$

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

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"
...

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

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

$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!

Statement
This article is reproduced at:ITcool. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool