/Etc/bashrc and/etc/profile cannot be clearly divided?

Source: Internet
Author: User

/Etc/bashrc and/etc/profile cannot be clearly divided?

In general linux or unix systems, you can edit bashrc and profile to set the user's work environment. Many articles also use profile and bashrc, but what is the role of each file and how to use it?

First, let's look at these files in the system. Generally, the system may have

/etc/profile/etc/bashrc~/.bashrc~/.profile

If the system is ubuntu or debian, there will be no/etc/bashrc and the/etc/bash. bashrc file.
These are common profile and bashrc files. Before you understand these files, you also need to understand the Shell, Shell login (login) and interactive (interactive) modes.

Shell Classification

There are many shell types in the system, such as bash, sh, and zsh. If you want to view the shell used by a user, you can use the finger [USERNAME] command to view the shell. here we only talk about shell as bash, because if it is sh or other shells, it will not obviously run bashrc.

Login shell and no-login shell

"Login shell" indicates user login. For example, if you use the "su-" command or use ssh to connect to a server, login shell mode is started by default.
In this mode, the shell will automatically execute the/etc/profile and ~ /. Profile file, but does not execute any bashrc file, so generally/etc/profile or ~ /. Profile, we will manually go to the source bashrc file.
In the case of no-login shell, we directly input bash or bash-c "CMD" in the terminal to start the shell.
In this mode, no profile files are automatically run.

Interactive shell and non-interactive shell

Interactive shell is an interactive shell, which is used to interact with users as the name suggests. It provides a command prompt to enter commands.
In this mode, an environment variable named PS1 exists. If it is not login shell, it will go to source/etc/bash. bashrc and ~ /. Bashrc File
Non-interactive shell is usually executed through bash-c "CMD.

In this mode, no rc files will be executed, but there is still a special situation that I will detail later

Execute the RC file in a possible Mode Combination

Enable the terminal in SSH login, sudo su-[USER] or mac
Ssh login and su-are typical interactive login shell, so there will be a PS1 variable, and will execute

/etc/profile~/.profile
Enter bash or ubuntu in the command prompt to open the terminal by default.

In this case, interactive no-login shell is enabled, so there will be a PS1 variable, and only

/etc/bash.bashrc~/.bashrc

Shell executed using the bash-c "CMD" or bash BASHFILE command

These commands will not be executed, that is, setting the PS1 variable without executing any RC file.
The most special! Commands executed through "ssh server CMD" or remote commands executed through programs

This is the most special mode. Theoretically it should be non-interactive or non-login, but in fact it will not set PS1, but it will still execute

/etc/bash.bashrcbashrc

It is worth noting that/etc/bashrc will not be executed under any circumstances.

Differences between bashrc and profile

After reading the preceding combinations of states, what is the difference between bashrc and profile?

Profile

In fact, you can get a rough idea about the name. profile is the only place a user can use to set environment variables, because the user can have multiple shells, such as bash, sh, and zsh, however, environment variables only need to be initialized in a unified place, and this is the profile.

Bashrc

Bashrc is used to initialize bash, for example, to initialize bash settings, bash Code Completion, bash alias, and bash color. and so on, there will also be shrc, and files like zshrc exist, but bash is too common.
Expected execution sequence
=> Indicates source in the file, line feed => indicates source after execution, and the same line indicates that source is executing itself first.

Common login shell
/etc/profile   => /etc/bash.bashrc~/.profile  => ~/.bashrc => /etc/bashrc
Run bash directly on the terminal
/etc/bash.bashrc~/.bashrc => /etc/bashrc
Bash-c "CMD" does not execute anything
ssh server “CMD”/etc/bash.bashrc => /etc/profile~/.bashrc => | /etc/bashrc => /etc/profile             | ~/.profile

Here it will be a little confusing, because there are both/etc/bash. bashrc has/etc/bashrc. In fact, ubuntu and debian have/etc/bash. bashrc file but no/etc/bashrc, other systems are basically only/etc/bashrc no/etc/bash. bashrc.

Final Modification

To achieve the process we need, we must modify the rc file of the system. We will use Ubuntu as an example.
First, add/etc/profile to the file header.

export system_profile_loaded=1

In this way, other files can be determined based on $ system_profile_loaded to determine whether the profile has been loaded. Then we can see

  unset ifiif [ "$PS1" ]; then  if [ "$BASH" ]; then    PS1='\u@\h:\w\$ '    if [ -f /etc/bash.bashrc ]; then    . /etc/bash.bashrc    fi  else    if [ "`id -u`" -eq 0 ]; then      PS1='# '    else      PS1='$ '    fi  fifiumask 022

According to our scheme, bashrc should be loaded at the end of the file no matter what the situation is, so we modify it

  unset ifiumask 022if [ "$BASH" ]; then  if [ "$PS1" ]; then    PS1='\u@\h:\w\$ '  fi  if [ -f /etc/bash.bashrc ]; then    . /etc/bash.bashrc  fielse  if [ "`id -u`" -eq 0 ]; then    PS1='# '  else    PS1='$ '  fifi

Of course, there can also be other methods, as long as it complies with the bashrc load at the end of the file.
Next, we modify/etc/bash. bashrc and add it to the file header.

[ -n "${system_bashrc_running}" ] && returnsystem_bashrc_running=1[ -z "${system_profile_loaded}" ] && source /etc/profileunset system_bashrc_runningsystem_bashrc_runned=1

Among them, system_bashrc_running and other variables are added to prevent two repeated calls.
In this way, the system-level rc files are basically modified, and it is best to modify the local rc file again, so we edit "~ /. Profile ", found content is

# ~/.profile: executed by Bourne-compatible login shells.if [ -n "$BASH" ]; then  if [ -f ~/.bashrc ]; then    . ~/.bashrc  fifimesg n

According to the above modification rules, you only need to replace

export local_profile_loaded=1if [ -n "$BASH" ]; then  if [ -f ~/.bashrc ]; then    . ~/.bashrc  fifi

In this way, we will always load bashrc after loading the profile, and then modify it like editing/etc/bash. bashrc ~ /. Bashrc. Add it to the file header.

[ -n "${local_bashrc_running}" ] && returnlocal_bashrc_running=1[ -r /etc/bashrc -a -z "${system_bashrc_runned}" ] && source /etc/bashrc[ -r ~/.profile -a -z "${local_profile_loaded}" ] && source ~/.profileunset local_bashrc_running

It is used to prevent repeated loading of profiles, and the special note here is

[ -r /etc/bashrc -a -z "${system_bashrc_runned}" ] && source /etc/bashrc

The/etc/bashrc file is only available in mac and other systems. Therefore, this line of ubuntu does not need to be added, but it does not matter if it has been added.
Here, the shell loading sequence cannot be solved perfectly. Of course, zsh is used by this user and needs to be modified according to the type principle.
In addition, there may be ~ /. Bash_profile ,~ Files such as/. bash_login, but bash will not load these files ~ /. Profile. If yes, delete these files and merge the files ~ /. Profile and ~ /. Bashrc.

From: http://www.linuxeye.com/Linux/bashrc-profile.html

Address: http://www.linuxprobe.com/diff-bashrcprofile.html


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.