What files do Linux environment variables come from?
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)
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.
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
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

The key differences between CentOS and Ubuntu are: origin (CentOS originates from Red Hat, for enterprises; Ubuntu originates from Debian, for individuals), package management (CentOS uses yum, focusing on stability; Ubuntu uses apt, for high update frequency), support cycle (CentOS provides 10 years of support, Ubuntu provides 5 years of LTS support), community support (CentOS focuses on stability, Ubuntu provides a wide range of tutorials and documents), uses (CentOS is biased towards servers, Ubuntu is suitable for servers and desktops), other differences include installation simplicity (CentOS is thin)

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

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)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.
