Linux Learning--the difference between Su and Su--deep analysis

Source: Internet
Author: User

the difference between Su-and Su--Interactive logon and non-interactive login

First, Brief introduction

Interactive : Enter the account password directly through the terminal login, etc.

variables, aliases, etc. won't inherits from the previous shell, it will be re-read

non-interactive : Open terminal under graphical interface, execute script, any other bash instance, etc.

variables, aliases, etc. will be inherit the upper-level shell and then read the file

Ii. Related Documents

Let's take a look at these five files. Main contents

/etc/profile : Storing environment variables that already exist in the system, such as Path,user

/etc/profile.d/*.sh : Storage of many color settings, vim settings, etc.

~/.bash_profile : The contents of the file refer to ~/.BASHRC

~/.BASHRC : The contents of the file refer to/ETC/BASHRC

/ETC/BASHRC : Setting variables related content

Profile class The shell of the interactive logon provides the configuration
BASHRC class Non-interactive and interactive logon shell provides configuration


File read order when user logs in:

Interactive: /etc/profile --/etc/profile.d/*.sh--and ~/.bash_profile--~/.BASHRC--/ETC/BASHRC

non-interactive: ~/.BASHRC ---/ETC/BASHRC-/etc/profile.d/*.sh

Three, combining case analysis

① Add export a=a1 to/etc/profile

Under/etc/profile.d/vim mage.sh, add content to export B=B2

Add Export c=c3 to ~lisi/.bash_profile (make sure Lisi this user exists)

Add Export d=d4 under ~LISI/.BASHRC

Add Export E=e5 under/ETC/BASHRC

Note: All variables are added in the first row, not including comment lines

② Re-login Su-root

Echo $A, Echo $B, Echo $C, Echo $D, Echo $E (currently login root user, according to the file read order, so will skip ~lisi/.bash_profile and ~LISI/.BASHRC)

Results show: A1 B2 E5

③/etc/profile A1 changed to A6

Echo $A, Echo $B, Echo $C, Echo $D, Echo $E (A6 is not displayed because a is a saved or last logged-in variable)

Results show: A1 B2 E5

④SU Root

Echo $A, Echo $B, Echo $C, Echo $D, Echo $E (non-interactive login root, in the non-interactive read file order, will also skip ~LISI/.BASHRC, the current non-interactive (child shell) will inherit the previous level of login shell, so a is the value of A1)

results show: A1 B2 E5

⑤exit (exit the child shell and return to the upper shell, that is, Su Root enters a child shell)

Echo $A, Echo $B, Echo $C, Echo $D, Echo $E

Results show: A1 B2 E5

⑥vim/etc/profile.d/mage.sh Change B2 to B7

su Root (/etc/profile.d/mage.sh will be read in non-interactive order)

Results show: A1 B7 E5

⑦exit

Echo $A, Echo $B, Echo $C, Echo $D, Echo $E

Results show: A1 B2 E5

⑧su Lisi

Echo $A, Echo $B, Echo $C, Echo $D, Echo $E

Results show: A1 B7 D4 E5

⑨exit

Su-lisi

Echo $A, Echo $B, Echo $C, Echo $D, Echo $E (in interactive read order, and interactive does not inherit variables from the previous login shell)

Results show: A6 B7 C3 D4 E5

Small tip: execution scripts are also run under a child shell, which inherits the variables of the parent shell (non-interactive)

Parent shell cannot inherit files from child shell

The above has been said: execution scripts are also non-interactive logins, variables defined in the script do not take effect outside the script (under the parent shell)

but source+ Script or . + Script is to run under the current shell (under the parent shell) and the bash+ Script (under sub-shell) There are essential differences between the methods


Linux Learning--the difference between Su and Su--deep analysis

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.