This is the 8th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

On Linux, there are three file types that appear very frequently: profile, bash_profile, and bashrc.

Because of the name, many people will remember the role of these three types of documents, so today we will take a detailed inventory of the role of these three types of documents and the difference.

1 profile

1.1 role

Profile, path: /etc/profile, which is used to set system-level environment variables and startup programs. The configuration in this file takes effect for all users.

When the user logs in, the file is executed and the shell Settings are looked up from the configuration file in the /etc/profile.d directory.

1.2 Setting environment variables in a Profile

It is generally not recommended to add environment variables to the /etc/profile file, as the Settings added to this file will work for all users.

When we must add, we can add as follows:

For example, add an environment variable with the HOST value juejin.cn:

export HOST=juejin.cn
Copy the code

When added, it can be used at the end of the line; Number, also can not use.

A variable name can correspond to multiple variable values. Separate multiple variable values using:.

After environment variables are added, you need to log in again to make them take effect. You can also run the source command to force them to take effect immediately.

source /etc/profile
Copy the code

To check whether it takes effect, run the echo command:

$ echo $HOST
juejin.cn
Copy the code

2 bashrc

The bashrc file is used to configure functions or aliases. There are two levels of bashrc files:

  • The system level
  • User level

2.1 the system level

The system-level one is in /etc/bashrc and is valid for all users.

2.2 user level

The user-level is located at ~/.bashrc and is valid only for the current user.

The bashrc file is only valid for the specified shell type, and bashrc is only called by the bash shell.

3 bash_profile

The bash_profile file is stored in ~/. Bash_profile. This file is a user-level setting and can be understood as the profile directory of a certain user.

This file can also be used to configure environment variables and launch programs, but only for a single user.

Like profiles, bash_profile takes effect when the user logs in, and can also be used to set environmental modifications.

Unlike profiles, however, bash_profile only takes effect for the current user.

4 Summary of Differences

The differences between the three file types can be expressed in one sentence:

/etc/profile, /etc/bashrc is the system global environment variable setting;

Profile, ~/. Bashrc Private environment variables in the user’s home directory.

When you get a shell process while logging in to the system, it reads the environment profile as follows:

  1. First read the global environment variable Settings file/etc/profile, and then read additional set documents according to their contents, such as/etc/profile.dand/etc/inputrc;
  2. According to the user account, read in their home directory~/.bash_profile;
  3. Fails to read~/.bash_login;
  4. If it fails again, it is read~/.profile(These three document Settings are basically the same, only read on the priority relationship);
  5. Finally, it reads according to the user account~/.bashrc.

Both ~/.profile and ~/.bashrc can be customized, but ~/.profile can set the path and environment variables that are unique to the user. It can only be executed once during login.

~/. Bashrc is also a user specific configuration document, you can set the path, command alias, every shell script execution will use it once.