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

Mar 25, 2024 am 08:00 AM
linux environment variables

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!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

Difference between centos and ubuntu Difference between centos and ubuntu Apr 14, 2025 pm 09:09 PM

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)

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

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).

Centos options after stopping maintenance Centos options after stopping maintenance Apr 14, 2025 pm 08:51 PM

CentOS has been discontinued, alternatives include: 1. Rocky Linux (best compatibility); 2. AlmaLinux (compatible with CentOS); 3. Ubuntu Server (configuration required); 4. Red Hat Enterprise Linux (commercial version, paid license); 5. Oracle Linux (compatible with CentOS and RHEL). When migrating, considerations are: compatibility, availability, support, cost, and community support.

How to install centos How to install centos Apr 14, 2025 pm 09:03 PM

CentOS installation steps: Download the ISO image and burn bootable media; boot and select the installation source; select the language and keyboard layout; configure the network; partition the hard disk; set the system clock; create the root user; select the software package; start the installation; restart and boot from the hard disk after the installation is completed.

How to view the docker process How to view the docker process Apr 15, 2025 am 11:48 AM

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).

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

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.

What to do if the docker image fails What to do if the docker image fails Apr 15, 2025 am 11:21 AM

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.

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)

See all articles