Guide |
In the General Linux or Unix system, can be edited BASHRC and profile to set up the user's working environment, many articles for profiles and BASHRC are also used, but what is the role of each file and how to use it? |
First we look at these files in the system, the general system may have
/etc/ Profile / etc / BASHRC ~/. BASHRC ~/. Profile
And if the system is Ubuntu or Debian, there will be no/ETC/BASHRC and there will be/ETC/BASH.BASHRC files. These are the common profile and BASHRC files. Before you can understand these files, you need to know about the shell, the shell's login (login), and the interactive (interactive) mode.
Classification of shells
There are many kinds of shell in the system, such as bash, SH, zsh, and so on, if you want to see what a user is using, the shell can be viewed by the finger [USERNAME] command. What we're saying here is that the shell is bash, because if it's sh or another shell, it's obviously not going to run BASHRC.
login shell and no-login shell
"Login Shell" on behalf of the user login, such as using the "Su-" command, or SSH to a server, will use the user's default shell to launch login shell mode. The shell in this mode will automatically execute the/etc/profile and ~/.profile files, but will not execute any of the BASHRC files, so generally/etc/profile or ~/.profile we will manually go to source bashr C file. The case of the No-login shell is the shell we launched directly under the terminal with bash or bash-c "CMD". This mode is not automatically run any profile files.
Interactive shell and non-interactive shell
The interactive shell is an interactive shell, which, as its name implies, is used to interact with the user, providing a command prompt to enter commands. In this mode, there will be an environment variable called PS1, if not the login shell will go to SOURCE/ETC/BASH.BASHRC and ~/.BASHRC file non-interactive shell is generally through bash-c " CMD "to perform the bash.
No RC files are executed in this mode, but there is a special case that I'll tell you about later.
execution of RC files in a possible pattern combination
SSH login, sudo su-[USER] or Mac open terminal SSH login and Su-is a typical interactive login shell, so there will be PS1 variable, and will execute
/etc/ Profile ~/. Profile
at the command prompt, enter bash or Ubuntu default settings to open the terminal
This opens the interactive no-login shell, so there will be PS1 variables that will only execute
/etc/ Bash . BASHRC ~/. BASHRC
Shell executed by bash-c "CMD" or bash bashfile command
These commands do nothing, that is, set the PS1 variable, do not perform any RC file the most special! Commands that are executed through SSH server CMD or that are executed remotely through a program
This is the most special kind of pattern and should theoretically be both non-interactive and non-logged, but in fact he doesn't set PS1, but it does
/etc/bash.bashrcbashrc
It is also worth noting that/ETC/BASHRC will not execute under any circumstances.
the difference between BASHRC and profile
After looking at the various state combinations, the key question is, what is the difference between BASHRC and profile?
Profile
In fact, the name can be understood, profile is the only place for a user to set the environment variables, because the user can have multiple shells such as bash, SH, zsh, etc., but like environment variables in fact, it only needs to be initialized in a unified place, and this is Profil E.
BASHRC
BASHRC is also known by the name to be used to initialize bash, such as the settings used to initialize bash, bash's code completion, bash alias, bash color. And so on there will be SHRC, ZSHRC such a file exists, but bash is too common. The desired execution order, = = is the source within the file, and the + = = Represents the end of itself after the execution of source, the same line represents the first source in executing itself
Common Login Shell
/etc/ Profile = / etc / Bash . BASHRC ~/. Profile = ~ /.BASHRC =/ etc / BASHRC
terminal species Run bash directly
/etc/ Bash . BASHRC ~ /.BASHRC =/ etc / BASHRC
Bash-c "CMD" doesn't do anything.
SSH server "CMD"/ETC/BASH.BASHRC =/ETC/PROFILE~/.BASHRC = | /ETC/BASHRC =/etc/profile | ~/.profile
There will be a little confusion here, because both/ETC/BASH.BASHRC and/ETC/BASHRC, in fact, Ubuntu and Debian have/ETC/BASH.BASHRC files but no/ETC/BASHRC, the other system is basically only /ETC/BASHRC no/ETC/BASH.BASHRC.
Final Modification
In order to achieve the above-mentioned implementation process, it is necessary to modify the system's RC files. We take Ubuntu For example first we edit/etc/profile in the file header join
Export system_profile_loaded = 1
So that other files can be based on the $system _profile_loaded to determine whether the profile has been loaded, and then we can see
unset ifiif [ "$PS 1" ]; Then if [ "$BASH" ]; ThenPS1=' \[email protected]\h:\w\$ ' if [ -F/etc/Bash.BASHRC]; Then . /etc/Bash.BASHRCfi Else if [ "' Id-u '" -eq0 ]; ThenPS1=' # ' ElsePS1=' $ ' fi fifiumask022
According to our plan just now, should be in any case should be at the end of the file to load BASHRC, so we modified to
unset ifiumask022if [ "$BASH" ]; Then if [ "$PS 1" ]; ThenPS1=' \[email protected]\h:\w\$ ' fi if [ -F/etc/Bash.BASHRC]; Then . /etc/Bash.BASHRCfiElse if [ "' Id-u '" -eq0 ]; ThenPS1=' # ' ElsePS1=' $ ' fifi
Of course, there can be other methods, as long as it fits at the end of the file loading BASHRC. Then we modify the/ETC/BASH.BASHRC and we need to add the file header to the
[-"${system_bashrc_running}"] && return system_bashrc_running = 1 [-"${system_profile_loaded}"] && /etc/profileunset system_bashrc_runningsystem_bashrc_runned =1
Variables such as system_bashrc_running are added to prevent 2 repeated calls. This system-level RC file is basically modified, it is better to modify the local RC file, so we edit "~/.profile", found that the content is
# ~/.profile:executed by bourne-compatible login shells. if [-"$BASH"]; then if [-~/. ]; then . ~/. BASHRC fi fi MESG N
And in accordance with the above-mentioned amendment rules only need to replace
Export local_profile_loaded = 1 if [-"$BASH"]; then if [-~/. ]; then . ~/. BASHRC fi fi
This will always be loaded after the profile to load the BASHRC, and then we like the editor/ETC/BASH.BASHRC to modify the ~/.BASHRC, the file header to add
[ -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 the profile from being loaded repeatedly, and it needs special note
[-/etc/ -- "${system_bashrc_runned}"&&/etc/ BASHRC
/ETC/BASHRC This file is only in the Mac and other systems, so Ubuntu here can not add, but there is a judgment whether there is no problem with adding. To this point basically can be more perfect solve the problem of the shell loading order, of course, for example, this user is zsh and the like also need to follow the principle of the type to modify. In addition, in the user directory may exist ~/.bash_profile, ~/.bash_login such files, but if there are these files bash will not be loaded ~/.profile, so if there is a need to delete these files and merge the contents into the ~/.prof Ile and ~/.BASHRC.
Originally from: http://www.linuxprobe.com/diff-bashrcprofile.html
Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/
/ETC/BASHRC and/etc/profile silly to know?