Analyzing the function difference between BASHRC and profile files in Linux system

Source: Internet
Author: User

When you use terminal ssh to log on to the console of the Linux operating system, a hint sign (for example: #或 ~) can be entered after the cue symbol, and Linux responds with the input command, a series of actions that are handled by a so-called shell.
The shell is a program, and the most common is bash, which is the shell that the login system will use by default.
BASHRC and profile are Shell's startup settings files (in fact, these two files are also shell scripts) that can initialize environment variables for the current shell and so on, similar to the Autoexec.bat files under Microsoft Dos/windows.
The use and differences of BASHRC and profile are described in detail below:
For individual users
User home Directory/.BASHRC

The code is as follows:
# ~/.bashrc:executed by Bash (1) for non-login shells.
# ~/.bashrc:executed by Bash (1) for non-login shells.

User home Directory/.profile

The code is as follows:
# ~/.profile:executed by bourne-compatible login shells.
# ~/.profile:executed by bourne-compatible login shells.

For all users

The code is as follows:
/etc/bash.bashrc
# system-wide. bashrc file for interactive bash (1) shells.
/etc/profile
#/etc/profile:system-wide profile file for the Bourne shell (SH (1)

The code is as follows:

/etc/bash.bashrc
# system-wide. bashrc file for interactive bash (1) shells.
/etc/profile
#/etc/profile:system-wide profile file for the Bourne shell (SH (1)

The difference between BASHRC and profile

As you can see from the English description above, the difference between BASHRC and profile is:
1. BASHRC is automatically run when the system is started.
2. Profile is not run until the user logs in.
3. After setting up, you can use source BASHRC command to update BASHRC, you can also use the source Profile command to update profile.
PS: Usually we modify BASHRC, some Linux distributions do not necessarily have profile this file
4. The variables set in the/etc/profile (global) can be applied to any user, while the variables set in ~/.BASHRC (local) only inherit the variables in/etc/profile, they are "parent-child" relationships.

To understand the difference between BASHRC and profile, first figure out what is an interactive shell and a non-interactive shell, and what is the login shell and non-login shell.

The interactive pattern is that the shell waits for your input and executes the commands you submit. This pattern is called interactive because the shell interacts with the user. This pattern is also familiar to most users: Log in, execute commands, and sign out. When you sign back, the shell is terminated. The shell can also run in another mode: non-interactive mode. In this mode, the shell does not interact with you, but instead reads the commands stored in the file and executes them. When it reads the end of the file, the shell terminates.

BASHRC and profile are used to save the user's environment information, BASHRC for interactive Non-loginshell, and profiles for interactive login shells. There are many BASHRC and profile files in the system, described below:

/etc/pro This file sets the environment information for each user of the system, and the file is executed when the first user logs on.

and collects the shell's settings from the/ETC/PROFILE.D directory's configuration file.

/ETC/BASHRC: This file is executed for each user running the bash shell. When the bash shell is opened, the file is read. Some Linux versions have no BASHRC files in the/etc directory.

~/. Pro Each user can use this file to enter the shell information dedicated to their own use, when the user logs on, the

The file is only executed once! By default, it sets some environment variables and then executes the user's. bashrc file.

~/.BASHRC: This file contains bash information dedicated to a user's bash shell, which is read when the user logs on and each time a new shell is opened.

In addition, the variables set in the/etc/profile (global) can be applied to any user, while the variables set in ~/.BASHRC, such as local, can only inherit the variables in/etc/profile, they are "parent-child" relationships.

Supplementary introduction
In addition, several documents related to BASHRC are needed to be added:
~/.bash_profile: Each user can use this file to enter shell information dedicated to their own use, and the file is executed only once when the user logs on! By default, he sets some environment variables to execute the user's. bashrc file.
~/.bash_logout: Executes the file every time you exit the system (the Bash shell is exited).
~/.bash_profile is an interactive, login-like way into bash, ~/.BASHRC is run interactively non-login into bash, which is usually set roughly the same, so usually the former will invoke the latter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.