search
HomeSystem TutorialLINUXThe usage of environment variables in Linux is divided by the same life cycle

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

~/.bashrc usually contains the following code:

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

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!

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中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可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

手机远程linux工具有哪些手机远程linux工具有哪些Apr 29, 2022 pm 05:30 PM

手机远程linux工具有:1、JuiceSSH,是一款功能强大的安卓SSH客户端应用,可直接对linux服务进行管理;2、Termius,可以利用手机来连接Linux服务器;3、Termux,一个强大的远程终端工具;4、向日葵远程控制等等。

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尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.