A brief introduction to Linux environment variable files

A brief introduction to Linux environment variable files

In the Linux system, environment variables can be roughly divided into system-level environment variables and user-level environment variables according to their scope of application.

  • System-level environment variables: Every user who logs into the system can read system-level environment variables
  • User-level environment variables: Each user who logs into the system can only read his own user-level environment variables

Naturally, the configuration files of environment variables are also divided into system-level and user-level ones.

System Level

/etc/profile

It runs when the first user logs in after the system starts, and collects shell settings from the configuration files in the /etc/profile.d directory. The environment variables configured using this file will be applied to every user who logs in to the system.

Tip: In Linux system, use the following command to make the configuration file take effect immediately.

source /etc/profile
echo $PATH

/etc/bashrc (/etc/bash.bashrc in Ubuntu and Debian)

It runs when the bash shell is opened. Modifying the environment variables configured in this file will affect the bash shell used by all users.

Note: There are different categories of bash shells here, and the environment variable configuration files used by different categories are also different. Normally, a non-login shell will not execute any profile files, and a non-interactive shell mode will not execute any bashrc files.

# Login shell and non-login shell:
Login shell # You need to enter the user password. For example, ssh login or su - command privilege escalation will start the login shell mode non-login shell # No need to enter the user password;
 
# Interactive shell and non-interactive shell:
Interactive shell # Providing a command prompt to wait for the user to enter commands is interactive shell mode Non-interactive shell # Directly running a script file is non-interactive shell mode

/etc/environment

It runs when the system starts. It is used to configure environment variables that are related to system operation but not related to the user. Modifying the environment variables configured in this file will affect the global environment.

User Level

~/.profile (recommended)

This file is executed when the user logs in. Each user can use it to configure their own shell information.

~/.bashrc

The file will be read when the user logs in and each time a new shell is opened. It is not recommended to configure user-specific environment variables here, because the file will be read once each time a shell is opened, which will definitely affect efficiency.

~/.bash_profile or ~./bash_login

~/.bash_profile or ~./bash_login - If one of these files exist, bash executes it rather then "~/.profile" 
when it is started as a login shell. (Bash will prefer "~/.bash_profile" to "~/.bash_login"). 
However, these files won't influence a graphical session by default.

The above is the description of ~/.bash_profile and ~./bash_login given by Ubuntu official website, translated into Chinese:

~/.bash_profile or ~./bash_login -
If one of these files exists, Bash executes that file instead of ~/.profile when starting a login shell.
If both files exist, Bash will prioritize ~/.bash_profile over ~/.bash_login.
However, by default these files do not affect graphical sessions.

~/.bash_logout

This file is executed every time you exit the system (exit the bash shell).

Note: Linux system uses $VARIABLE_NAME to access environment variables, and multiple environment variables are separated by :. Windows system uses %VARIABLE_NAME% to access environment variables, and multiple environment variables are separated by ;.

Generally, the execution order of Linux loading environment variable configuration files is:

==> /etc/profile
==> ~/.bash_profile | ~/.bash_login | ~/.profile
==> ~/.bashrc
==> /etc/bashrc
==> ~/.bash_logout

This is the end of this article about the introduction of Linux environment variable files. For more relevant Linux environment variable files, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to configure environment variables in Linux environment
  • Summary of Linux environment variable configuration methods (differences between .bash_profile and .bashrc)
  • How to configure Bash environment variables in Linux
  • Detailed explanation of Linux environment variable configuration strategy
  • A complete guide to Linux environment variable configuration
  • How to configure Java environment variables in Linux system
  • Installation and configuration of Java environment variables under Linux
  • Detailed steps for configuring environment variables in Linux
  • A brief discussion on how to modify/set the environment variable JAVA_HOME under Linux

<<:  MySQL multi-table join introductory tutorial

>>:  JavaScript to achieve custom scroll bar effect

Recommend

Comparative Analysis of IN and Exists in MySQL Statements

Background Recently, when writing SQL statements,...

Tips for data statistics in MySQL

As a commonly used database, MySQL requires a lot...

Several common ways to deploy Tomcat projects [tested]

1 / Copy the web project files directly to the we...

How to install mysql via yum on centos7

1. Check whether MySQL is installed yum list inst...

Detailed explanation of Nginx forwarding socket port configuration

Common scenarios for Nginx forwarding socket port...

Detailed explanation of how to configure openGauss database in docker

For Windows User Using openGauss in Docker Pull t...

CUDA8.0 and CUDA9.0 coexist under Ubuntu16.04

Preface Some of the earlier codes on Github may r...

JavaScript canvas to achieve code rain effect

This article shares the specific code for canvas ...

React hooks pros and cons

Table of contents Preface advantage: shortcoming:...

A brief discussion on the corresponding versions of node node-sass sass-loader

Table of contents The node version does not corre...

The visual design path of the website should conform to user habits

Cooper talked about the user's visual path, w...