Understanding of environment variables in profile and BASHRC under Ubuntu

Source: Internet
Author: User

(0) write in front

Some nouns may need to be explained. (You can also look at this section first, see the doubts later on the explanation)

Launch bash Shell: A bash shell process is started, which is usually understood to be opening a terminal. Note that if you enter SH after the terminal will find yourself into another interactive interface, this time actually fork a shell sub-process, if you enter an SH in this interactive interface, then the equivalent of the fork shell sub-process and fork a shell subprocess , three bash shell processes are launched at this time.

Enter exit or ctrl-d to exit the current shell, where you need to exit two consecutive times to return to the original terminal shell process (this time back to a shell process).

$PS 1 and Interactive run (running interactively): Simply put, the interactive operation is to enter instructions on the terminal to run, non-interactive operation is to execute the SH file. $PS1 is valued when running interactively. Non-interactive runs do not perform the configuration of the BASHRC file, which needs to be noted, since it is common to execute instructions on the terminal, it is easy to ignore this: what is configured in BASHRC is invalidated when the sh file is executed.

Source profile or source BASHRC:Source an sh file that loads the contents of the sh file into the shell environment for execution. It can be understood that the execution of SH is non-interactive and that the terminal source SH file is equivalent to the instruction of the terminal to execute the SH file, which is to run interactively. (after this sentence for the moment as a conjecture, because have not yet done experimental validation, back to verify a wave)

(1) Profile and BASHRC

The configuration environment variables are typically in both of these files. Let's talk about when to execute, and then describe what the two files do.

Profile executes after the system is logged on, only once when the system is logged on, including /etc/profile for the system and ~/.profile for the user.

BASHRC is executed every time the bash shell is launched(opening the terminal or entering SH at the terminal), including /ETC/BASH.BASHRC for the system and ~/.BASHRC for the user (note here that my Ubuntu is/ETC/BASH.BASHRC, other systems may be/ETC/BASHRC)

cat /etc/profilecat /etc/bash.bashrccat ~/. Profiles Cat ~/.BASHRC

(2) Environment variables

Because you want to configure environment variables, you should be aware of the environment variables. Environment variables in the shell are divided into global variables and local variables.

blogger="piligrimhui"   is defined as local variable export blogger="Pilgrimhui  " This is defined as a global variable (export this variable, then promoted to a global variable)

2.1 Local variable: The local variable defined by the parent process, the child process cannot access the local variable defined by the child process, and the parent process cannot access it.

 # Parent. sh  # !/bin/bashpid  =  " parent   " sh  child. sh  echo   "  The parent shell accesses the CID of the child shell: $cid   " 
# Child. SH #!/bin/bashecho" child shell accesses the PID of the parent shell: $pid"cid= "  Child "
SH parent. SH the child shell accesses the PID of the parent shell: The parent shell accesses the CID of the child shell:

2.2 Global variables: The global variables defined by the parent shell, the child shell itself copies a copy of the global variables of the parent shell, so the operation of the child shell on the global variable does not affect the global variables of the parent shell. A child shell defines a global variable that is not available to the parent shell.

 # Parent. sh  # !/bin/bashexport name  ="  parent   " echo   "  The name of the parent shell : $name   " sh  child.sh  echo  "   After the child shell modifies name, the parent shell's name: $name   echo  "   Parent shell accesses child shell-defined nickname: $nickName   " 
 # Child. sh  # !/bin/bash  echo   "  child shell name: $name   " name  = "  Child   echo   "  After the child shell modifies name, the child shell name: $name   " nickname  = "  Baby   " 
SH parent. SH after the Name:parent child shell of the name:parent child shell of the parent shell modifies name, the child Shell's Name:child child shell modifies name after the parent shell's name: Parent Parents shell accesses the nickname defined by the child shell:

(3) What does profile do?

The login shell starts with the user's login and can be seen as the first shell, and the subsequent shell is the child shell of the login shell.

The login shell executes the /etc/profile for the system and the ~/.profile for the user. In order for the environment variable to be accessible to all subsequent shells, the Global environment variable can be configured here, but note that profile is only executed once at login time, so the general configuration needs to be re-logged in order to take effect. (although you can self source profile But only in the current shell process is valid, the shell process here can be understood to be in a terminal, but if the terminal input SH is actually equivalent to open two shell process, A parent process a child process)

For/etc/profile, the first check is whether to run interactively (that is, $PS1 is not empty), whether to launch the bash shell, and if so, to perform/ETC/BASH.BASHRC to configure bash. The shell files are then executed in the/ETC/PROFILE.D directory, some as self-starters, and some to define some global environment variables.

For ~/.profile, first check if the bash shell is started, and if so, execute ~/.BASHRC to configure the bash. The path is then reset (so the environment variable for different users is not the same as the path).

#/etc/profile:system-wide. Profilefile  forThe Bourne Shell (SH(1) # and Bourne compatible shells (Bash (1), Ksh (1), Ash (1), ...).if["$PS 1"]; Then  if["$BASH"] && ["$BASH"!="/bin/sh"]; Then# thefileBASH.BASHRC already sets the default PS1. # PS1='\h:\w\$'    if[-F/ETC/BASH.BASHRC]; Then      . /etc/BASH.BASHRCfi  Else    if["' Id-u '"-eq0]; ThenPS1='# '    ElsePS1='$ '    fi  fifiif[-D/ETC/PROFILE.D]; Then   forIinch/etc/profile.d/*. Sh; do if [-R $i]; $i fi done unset IFI
# ~/.profile:executed by the command interpreter for Loginshells.# ThisfileIs isn't read by bash (1),if~/.bash_profile or ~/. bash_login# exists.# See/usr/share/doc/bash/examples/startup-files forexamples.# The files are locatedinchThe bash-Doc package.# The default umask is setinch/etc/profile; forSetting the umask# for SSHLoginsInstalland configure the libpam-umask package. #umask022# ifRunning Bashif[-N"$BASH _version"]; Then# include. BASHRCifIt existsif[-F"$HOME/.BASHRC"]; Then    . "$HOME/.BASHRC"    fifi# Set PATH so it includes user's Private Bin directoriesPath="$HOME/bin: $HOME/.local/bin: $PATH"

(4) What did BASHRC do?

/ETC/BASH.BASHRC and ~/.BASHRC are executed when the bash shell is launched (opening the terminal).

These two files are also executed when/etc/profile and ~/.profile are checked for execution by the bash shell (for/etc/profile to check for interactive runs first).

Let's see what these two bashrc have done.

For/ETC/BASH.BASHRC: First check whether to run interactively, not just do nothing. Yes, the back is a bunch of messy operations.

For ~/.BASHRC: First check whether to run interactively, not just do nothing. Yes, behind a bunch of messy operations, some of which have alias operations, our usual ll is set here, is the alias of Ls-alf.

Because the shell process starts every time it is executed here, it is generally possible to configure environment variables later.

The most common configuration method is vim ~/.BASHRC then add the configuration of the environment variable in the last few lines, then source ~/.BASHRC or restart the terminal.

file  for Interactive bash (1onfileforlogin   File in/etc/profile.# If not running interactively, and Don't do anything " $PS 1 " ] && return ...
# ~/.bashrc:executed by Bash (1) fornon-Loginshells.# See/usr/share/doc/bash/examples/startup-files (inchThe package bash-Doc) # forexamples# If not running interactively, Don't do anything Case$-inch*i*) ;; *) return;;Esac... # some More lsAliasesalias ll='Ls-alf'alias La='ls-a'alias L='LS-CF'

(5) write in the back

Finally, the various Linux system profiles and BASHRC files are different, I use ubuntu16.04, other systems may be different, you can see the shell code inside to understand and practice validation.

This summary roughly straightened out the whole environment variable "circulation process", understand the process after the idea should be clear a lot, if there are errors, welcome to correct.

Understanding of environment variables in profile and BASHRC under Ubuntu

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.