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
What are the main tasks of a Linux system administrator?What are the main tasks of a Linux system administrator?Apr 19, 2025 am 12:23 AM

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Is it hard to learn Linux?Is it hard to learn Linux?Apr 18, 2025 am 12:23 AM

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

What is the salary of Linux administrator?What is the salary of Linux administrator?Apr 17, 2025 am 12:24 AM

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

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.

Does the internet run on Linux?Does the internet run on Linux?Apr 14, 2025 am 12:03 AM

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

What are Linux operations?What are Linux operations?Apr 13, 2025 am 12:20 AM

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Boost Productivity with Custom Command Shortcuts Using Linux AliasesBoost Productivity with Custom Command Shortcuts Using Linux AliasesApr 12, 2025 am 11:43 AM

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

What is Linux actually good for?What is Linux actually good for?Apr 12, 2025 am 12:20 AM

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.

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

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools