In which file are environment variables located in Linux?

青灯夜游
Release: 2022-06-10 19:02:00
Original
15218 people have browsed it

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

In which file are environment variables located in Linux?

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

  • System-level environment variables: Every user who logs in to the system can read system-level environment variables;
  • User-level environment Variables: Each user who logs in to the system can only read his own 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
Copy after login

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;

    • 1. bashrc will run automatically after the system starts.
    • 2. The profile will be run after the user logs in.
    • 3. After setting, you can use the source bashrc command to update bashrc, or you can use the source profile command to update the profile.
    • 4. The variables (global) set in /etc/profile can affect any user, while the variables (local) set in ~/.bashrc can only inherit those in /etc/profile. variables, they have a "father-son" relationship.

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!