Home > System Tutorial > LINUX > The usage of environment variables in Linux is divided by the same life cycle

The usage of environment variables in Linux is divided by the same life cycle

王林
Release: 2024-03-25 08:00:49
forward
725 people have browsed it

Preface:

1. Environment variables are variables set by the operating system environment and are applicable to user processes in the entire system;

2. Environment variables can be set in the command, and the setting value will be lost when the user logs out;

3. If you want to apply it repeatedly, it is best to define it in .profile; the use of environment variables is the same as the use of local variables,

4. And before use, it must be imported with the export command.

1. Introduction to environment variable files

Environment variables in Linux include system-level and user-level. System-level environment variables are system variables that every user who logs in to the system must read, while user-level environment variables are loaded when the user uses the system. Environment variables, so the files that manage environment variables are also divided into system level and user level.

2. Variable types in Linux

Defined by the life cycle of variables, Linux variables can be divided into two categories:

1. Permanent: The configuration file needs to be changed, and the variables will take effect permanently

2. Temporary: Use the export command to declare, the variable will become invalid when the shell is closed

3. Linux environment variable file

Linux variable files include /etc/environment or /etc/profile or ~/.profile or /etc/bash.bashrc or ~/.bashrc, and ~/.bash_profile (usually created by users in their own directories) ,~/.bash_logout.

System level:

1./etc/environment: is the first file read by the system when logging in. This file sets the environment of the entire system. This file will only be read when the system is started. redhat linux download is used for all The process sets environment variables. When the system uses this file, it does not execute the commands in this file. Instead, it follows the code in the KEY=VALUE mode and sets VALUE to the KEY parameter. Therefore, if you want to define the PATH environment variable in the file, you only need to add a line of the form: The code of PATH=$PATH:/xxx/bin can

2./etc/profile: This file is the second file executed when the system logs in. Set environment information for each user of the system. When the user logs in for the first time, this file is executed. And collect shell settings from the configuration file in the /etc/profile.d directory. (/etc/profile can be used to set environment variables for all users in the entire system. The environment variable period is permanent)

3./etc/bashrc: It is a bash initialization file for all users. The environment variables set here will be applied to the shells of all users. This file will be executed every time the user opens the shell. (That is, every time a new terminal is opened, /etc/bashrc will be executed).

User level (this file is in the home directory):

1.~/.profile: The profile file corresponding to the currently logged in user, used to customize the current user's personal working environment (the variable is permanent). Each user can use this file to enter a shell dedicated to their own use. Information, when the user logs in, this file is only executed once! By default, it sets some environment variables and executes the user's .bashrc file. This is the recommended place to put your personal settings

2.~/.bashrc: This file contains bash information specific to your bashshell. This file is read when logging in and every time a new shell is opened. (~/.bashrc is only for the current user, the life cycle of variables is permanent)

It is not recommended to put it here, because every time a shell is opened, this file will be read once. In the Linux system, what is the default shell? , the efficiency will definitely be affected.

3.~/.bash_profileor~./bash_login:~/.bash_profile is used to enter bash running in interactive login mode, ~/.bashrc is used to enter bash running in interactive non-login mode. Generally, the settings of the two are roughly the same. are the same, so generally the latter will call the former. Each user can use this file to enter shell information specific to their own use. When the user logs in, the file is only executed once. By default, it sets some environment variables and executes the user's .bashrc file. (If there is no .bash_profile in the ~/ directory, create a new one) This is the recommended place to place personal settings

When a shell is closed, the system variables defined in bash_profile will become invalid. To do this, sourcebash_profile must be run every time a new shell is opened. And for the current user.

4.~/.pam_environment: User-level environment variable setting file. I haven’t tested it, so I don’t know if it works.

5.~/.bash_logout: Execute this file every time you exit the system (exit bashshell). In addition, the variables (global) set in /etc/profile can affect any user, while the variables (local) set in ~/.bashrc can only inherit the variables in /etc/profile. They are "mother and child" "relation.

4. Linux environment variable file execution order

1. The process of executing files when logging into Linux is as follows:

When you first log in to LinuxIn the Linux system, what is the default shell,

First start the /etc/profile file,

After that, start one of the ~/.bash_profile, ~/.bash_login or ~/.profile files in the user directory. The order of execution of the files in the host directory is:

~/.bash_profile->~/.bash_login->~/.profile.

If the ~/.bash_profile file exists, the ~/.bashrc file will usually be executed.

Because there is usually the following code in the ~/.bash_profile file:

if [ -f ~/.bashrc ] ; then
 . ./bashrc
fi
Copy after login

~/.bashrc usually contains the following code:

if [ -f /etc/bashrc ] ; then
 . /etc/bashrc
fi
Copy after login

So, ~/.bashrc will call the /etc/bashrc file. Finally, when exiting the shell, the ~/.bash_logout file is executed.

The execution order is:/etc/profile->(~/.bash_profile|~/.bash_login|~/.profile)->~/.bashrc->/etc/bashrc->~/. bash_logout

The benefits of various environment variable setting files such as /etc/profile and /etc/environment

1) First add exportLANG=zh_CN to /etc/profile, exit the system and log in again. The login prompt will display Chinese.

2) First delete exportLANG=zh_CN in /etc/profile, add LNAG=zh_CN to /etc/environment, exit the system and log in again. The login prompt will display in English.

In the process of improving the user environment, /etc/profile is always executed first, and then /etc/environment is read.

Why are there differences as described above? Instead of executing /etc/environment first and then /etc/profile?

This is because: /etc/environment is to set the environment of the entire system, and /etc/profile is to set the environment of all users. The latter has nothing to do with the logged in user, and the former is related to the logged in user.

The execution of system applications may have nothing to do with the user environment, but is related to the system environment. Therefore, when you log in, the prompt information you see, such as the display format of date and time information, is consistent with the LANG of the system environment. Relatedly, the default LANG=en_US, if the system environment LANG=zh_CN, the prompt information is in English, otherwise it is in English.

For the user's shell initialization, /etc/profile is first executed, and then the file /etc/environment is read; for the entire system, /etc/environment is executed first. Is this correct understanding?

The order when logging into the system should be:

/etc/enviroment-->/etc/profile-->HOME/.profile−−>HOME/.env (if it exists)

/etc/profile is the environment variable for all users

/etc/enviroment is the system environment variable

The order of shell reading when logging into the system should be:

/etc/profile->/etc/enviroment-->HOME/.profile−−>HOME/.env

The reason should be the difference between the user environment and the system environment. If the same variable has different values ​​in the user environment (/etc/profile) and the system environment (/etc/environment), it should be based on the user environment. Right.

5. Common environment variables and their functions in Linux

Common environment variables in Linux are:

1.PATH: Specify the search path for the command

2.HOME: Specify the user's home working directory (that is, the default directory when the user logs in to the Linux system)

3.HISTSIZE: refers to the number of historical command records saved.

4.LOGNAME: refers to the login name of the current user.

5.HOSTNAME: refers to the name of the host. If many applications want to use the host name, it is usually obtained from this environment variable.

6.SHELL: refers to which Shell the current user is using.

7.LANG/LANGUGE: Language-related environment variables. Users who use multiple languages ​​can change this environment variable.

在linux系统,默认的shell是什么_在linux系统,默认的shell是什么_在linux系统,默认的shell是什么

8.MAIL: refers to the current user’s email storage directory.

9.PS1: The basic command prompt is # for root users and $ for ordinary users.

10.PS2: Auxiliary prompt, the default is ">".

Note: You can change the current command prompt by changing this environment variable. For example, the following command will change the prompt to the string "Hello,MyNewPrompt".

#PS1="Hello,MyNewPrompt"

Note: The names of the above variables are not fixed. For example, HOSTNAME may be set to HOST

in some Linux systems.

Actually, the above environment variables I listed are not exhaustive!

Linux also provides commands to change and view environment variables! Below are several examples to illustrate:

1.echo displays a certain environment variable value echo$PATH

2.export sets a new environment variable exportHELLO="hello" (can be without comma), and directly uses [export variable name = variable value] to define the variable under the shell command line. This variable is only in the current shell. It is valid under (BASH) or its subshell (BASH). When the shell is closed, the variable will become invalid. When you open a new shell, the variable will not exist. If you need to use it, you need to redefine it.

3.env displays all environment variables

4.set displays locally defined shell variables (already existing in the system) and new variable values ​​for setting shell variables. If no value is specified, the variable value will be set to NULL.

5.unset cleans up the environment variable unsetHELLO

6.readonly sets the read-only environment variable readonlyHELLO. If the readonly command is used, the variable cannot be changed or cleared

The above is the detailed content of The usage of environment variables in Linux is divided by the same life cycle. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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