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

JS realizes the case of eliminating stars

This article example shares the specific code of ...

What kinds of MYSQL connection queries do you know?

Preface If the query information comes from multi...

Implementing a distributed lock using MySQL

introduce In a distributed system, distributed lo...

Eight ways to implement communication in Vue

Table of contents 1. Component Communication 1. P...

Solution to 404 Problem of Tomcat Installation in Docker

Find the containerID of tomcat and enter the toma...

Detailed explanation of the working principle and solution of Js modularization

Table of contents 1. Modular concept 2. Modulariz...

React Hooks Common Use Scenarios (Summary)

Table of contents 1. State Hook 1. Basic usage 2....

Analysis of the principle and usage of MySQL continuous aggregation

This article uses examples to illustrate the prin...

mysql installer web community 5.7.21.0.msi installation graphic tutorial

This article example shares the specific code for...

JS Easy to understand Function and Constructor

Table of contents 1. Overview 1.1 Creating a func...

Detailed explanation of the use of Docker commit

Sometimes you need to install certain dependencie...

HTML implements read-only text box and cannot modify the content

Without further ado, I will post the code for you...

Mysql: The user specified as a definer ('xxx@'%') does not exist solution

During the project optimization today, MySQL had ...