From: https://wido.me/sunteya/understand-bashrc-and-profile/
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
12345 |
/etc/profile/etc/bashrc~/.bashrc~/.profile
|
If the system is Ubuntu or Debian, there will be no/etc/bashrc
However/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 pattern shell Classification
There are many shell types in the system, such as Bash, sh, zsh, etc. If you want to view what shell a user is using, you can usefinger [USERNAME]
Command to view.Here we only talk about the bash shell.Because if it is sh or other shells, it is clear that bashrc will not run.
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 runs automatically./etc/profile
And~/.profile
File, but does not execute any bashrc file, so the/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 modePS1
If the environment variable is not yet 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 SSH Login for the RC file in a possible mode combination,
sudo su - [USER]
Or enable the terminal on Mac.
SSH Login andsu -
Is a typical interactive Login Shell, so there will be a PS1 variable, and will execute
/etc/profile~/.profile
Enter
bash
Or enable the terminal in Ubuntu 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.bashrc~/.bashrc
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
=>
Source in the file, line feed=>
Indicates that the execution is completed on the source, and the same line indicates that the source is executed on the source
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"
Do 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.
Edit/etc/profile
Add in 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=‘\[email protected]\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=‘\[email protected]\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
, We need to add in 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, the bashrc will be loaded after the profile is loaded./etc/bash.bashrc
Same Modification~/.bashrc
, Add to 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
/etc/bashrc
This file is only available in MAC and other systems. Therefore, this line of ubuntu does not need to be added here. However, it does not matter whether the file exists.
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, it may exist in the user directory.~/.bash_profile
,~/.bash_login
Such a file, but if there is such a file, Bash will not load it~/.profile
So if the file exists, delete the file and merge the content~/.profile
And~/.bashrc
Only.
Finally, you can also refer to the https://github.com/sunteya/sot project, this project is my current use of the dot series file configuration. Inside the bashrc and profile are modified according to the above process.