There are multiple configuration files containing environment variables in Linux: 1. "/etc/profile" file. The environment variables configured in this file will be applied to every user who logs in to the system; 2. "/etc /bashrc", modifying the environment variables configured in this file will affect the bash shell used by all users; 3. "/etc/environment", this file contains environment variables related to system operation but not related to users; 4. "~/ .profile"; 5. "~/.bashrc".
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
1. Introduction to environment variables:
In the Linux system, environment variables can be roughly divided into system-level environment variables according to their different scopes. and user-level environment variables.
Naturally, the configuration files of environment variables are also divided into system-level ones accordingly. and user level.
2. System level:
1./etc/profile
Start in the system It runs when the first user logs in and collects shell settings from the configuration file 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 systems, use the following command to make the configuration file take effect immediately.
source /etc/profile echo $PATH
2./etc/bashrc (/etc/bash.bashrc in Ubuntu and Debian)
In bash shell When run when opened, modifying the environment variables configured in this file will affect the bash shell used by all users.
Note: The bash shell here has different categories, and the environment variable configuration files used by different categories are also different.
Generally, non-login shell will not execute any profile file, and non-interactive shell mode will not execute any bashrc file.
3, /etc/environment
is run when the system starts. It is used to configure environment variables related to system operation but not related to users. Modify the configuration of this file. Environment variables will affect the global environment.
3. User level:
1, ~/.profile (recommended first choice)
~/.profile: executed by Bourne-compatible login shells.
Executed when the user logs in. Each user can use this file to configure shell information exclusive to them.
2、~/.bashrc
~/.bashrc: executed by bash(1) for non-login shells.
This file will be read when the user logs in and every time a new shell is opened. It is not recommended to configure user-specific environment variables here, because every time a shell is opened, the file will be read. Reading once will definitely affect the efficiency.
Effectiveness: source
The difference between bashrc and profile
From the above English description, we can know that the difference between bashrc and profile is:
Note: Usually we modify bashrc, some Linux distributions may not have the profile file;
3、~/.bash_profile or ~./bash_login
##~/.bash_profile or ~ ./bash_login - If one of these file 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 are the instructions about ~/.bash_profile and ~./bash_login given by the ubuntu official website, translated into Chinese:
~/.bash_profile or ~./bash_login -
If one of the files exists If so, when a login shell is started, Bash will execute the file instead of ~/.profile;
If both files exist, Bash will execute ~/.bash_profile instead of ~/. bash_login ;
However, by default these files do not affect graphical sessions.
4, ~/.bash_logout
Execute this file every time you exit the system (exit the bash shell).
Note: Linux systems use $VARIABLE_NAME to access environment variables, and multiple environment variables are separated by ":". Windows systems use %VARIABLE_NAME% to access environment variables, and multiple environment variables are separated by ;.
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of In which file are environment variables located in Linux?. For more information, please follow other related articles on the PHP Chinese website!