Differences and functions of/etc/profile,/etc/bashrc and other files in centos

Source: Internet
Author: User
Tags aliases centos


When we perform some bash-related operations, such as setting aliases and logging on to startup items, we will deal with the following files more or less. When we use them, we will check them and forget them. A good memory is not as bad as a pen. The old master will help you understand what these files are.

/Etc/profile
/Etc/bashrc
~ /. Bash_profile
~ /. Bashrc

• File description:

Templates/ect/profile
■ This file sets the environment information for each user in the system. When the user logs on for the first time, this file is executed. and from/etc/profile. the configuration file in the d directory collects shell settings.
HANGZHOU/etc/bashrc
■ Execute this file for every user running bash shell. When bash shell is opened, the file is read.
Region ~ /. Bash_profile
■ Every user can use this file to input shell Information dedicated to his/her use. When a user logs on, this file is only executed once! By default, it sets some environment variables and runs ~ /. Bashrc file.
Region ~ /. Bashrc
■ The file contains bash information dedicated to the user's bash shell. The file is read at login and every time a new shell is opened.
Region ~ /. Bash_logout
■ Execute this file every time you exit the system (exit bash shell.


• In addition, the variables (global) set in/etc/profile can act on any user
• And ~ The variables (local) set in/. bashrc can only inherit the variables in/etc/profile. They are "parent-child" relationships.


Man bash ----- INVOCATION

Taking CENTOS as an example, other systems may be slightly different.

Log on to shell

A login shell is one whose first character of argument zero is a-, or one started with the -- login option.
Old and high annotations:

Determine whether a shell is used to log on to the shell, as long as you are using su-, bash-login. If you enter a terminal without logon, enter the user name and password, then it is a non-login shell. For example, if you open a terminal on a MAC, non-login shell does not need to be logged on.

Another method is echo $0. If you are logging on to the shell,-[bash name] is returned; otherwise, [bash name] is returned.

Interactive Shell

An interactive shell is one started without non-option arguments and without the-c option whose standard input and error are both connected to terminals (as determined by isatty (3 )), or one started with the-I option. PS1 is set and $-includes I if bash is interactive, allowing a shell script or a startup file to test this state.
Old and high annotations:

It is easy to judge an interactive shell. The echo $-result contains the lower-case letter I.

When you run a script, it is a non-interactive shell.
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. bash behaves as if the following command were executed:
If [-n "$ BASH_ENV"]; then. "$ BASH_ENV"; fi
But the value of the PATH variable is not used to search for the file name.
Old High translation:

When a non-interactive shell runs a shell script, it runs $ BASH_ENV in the environment variable. The code is if [-n "$ BASH_ENV"]; then. "$ BASH_ENV"; fi.

The following paragraphs describe how bash executes its startup files. if any of the files exist but cannot be read, bash reports an error. tildes are expanded in file names as described below under Tilde Expansion in the EXPANSION section.
When bash is invoked as an interactive login shell, or as a non-interactive shell with the -- login option, it first reads and executes commands from the file/etc/profile, if that file exists. after reading that file, it looks ~ /. Bash_profile ,~ /. Bash_login, and ~ /. Profile, in that order, and reads and executes commands from the first one that exists and is readable. The -- noprofile option may be used when the shell is started to inhibit this behavior.
Old High translation:

After you log on to an interactive shell, if/etc/profile exists, bash will first read the configuration of/etc/profile, and then read ~ /. Bash_profile ,~ /. Bash_login and ~ /. Profile.

When a login shell exits, bash reads and executes commands from the file ~ /. Bash_logout, if it exists.
Old High translation:

When a logon shell exits ~ /. Bash_logout exists and bash will read it.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~ /. Bashrc, if that file exists. This may be inhibited by using the -- norc option. The -- rcfile file option will force bash to read and execute commands from file instead ~ /. Bashrc.
Old High translation:

When a non-login shell runs, if the corresponding user's ~ /. Bashrc exists. bash first reads ~ /. Bashrc. There is a -- norc option that can block reading, and the -- rcfile option that can force read of the specified file.

To sum up:

~ /. Bashrc and/etc/bashrc ,~ /. Bash_profile has the same relationship with/etc/profile. It is a global setting for a user. It is also a global priority when reading data.

The profile is read during interactive shell logon and bashrc is read during initialization.

Non-interactive mode only reads bashrc.

Generally, alias and function are stored in bashrc or ~ /. Bashrc.

Put more export in the profile file.

We recommend that you refer to the configuration of My bashrc, bash aliases, profile and other files;

At the same time, install the best shell in Linux: zsh + oh-my-zsh the best replacement of the default shell;

If you still love bash, it is recommended that you install bash-completion to automatically complete the function!

Related Article

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.