Home System Tutorial LINUX What files do Linux environment variables come from?

What files do Linux environment variables come from?

Mar 20, 2024 pm 07:50 PM
linux environment variables red 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
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!

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

vscode terminal command cannot be used vscode terminal command cannot be used Apr 15, 2025 pm 10:03 PM

Causes and solutions for the VS Code terminal commands not available: The necessary tools are not installed (Windows: WSL; macOS: Xcode command line tools) Path configuration is wrong (add executable files to PATH environment variables) Permission issues (run VS Code as administrator) Firewall or proxy restrictions (check settings, unrestrictions) Terminal settings are incorrect (enable use of external terminals) VS Code installation is corrupt (reinstall or update) Terminal configuration is incompatible (try different terminal types or commands) Specific environment variables are missing (set necessary environment variables)

See all articles