1. 背景
在看shell变量的时候引起了兴趣: 局部变量,全局变量,环境变量,shell的配置文件,参考博客:
http://c.biancheng.net/view/773.html
2. 交互式与非交互式
参考博客:
shell的两个属性:是否交互式(interactive), 是否登录(login) - 知乎
man bash:
通常的理解是:
man手册中给我们提供了怎么判断交互与非交互的方法:
所以我看看我自己的shell是否是交互式的:
liaojunwu@liaojunwu-virtual-machine:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$ echo $-
himBH
liaojunwu@liaojunwu-virtual-machine:~$
3. 登录与非登陆
man bash:
通常的理解是:
su和su -命令的区别参考博客: 一分钟知识点:linux命令之su 和 su -
可以用shopt login_shell的方法来看看shell是否设置了login_shell选项,我看看我自己的shell:
liaojunwu@liaojunwu-virtual-machine:~$ shopt login_shell
login_shell off
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$ su - liaojunwu --login
密码:
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$ shopt login_shell
login_shell on
liaojunwu@liaojunwu-virtual-machine:~$
4. 为什么要知道这两种属性?
因为涉及到shell配置方式的不同,继续看man手册:
交互与登录属性一共有四种组合,我们只看三种:
登录交互与登录非交互: bash配置shell的顺序是:
/etc/profile->~/.bash_profile->~/.bash_login->~/.profile->~/.bashrc
展开/etc/profile又可以看到登录交互会去配置/etc/bash.bashrc
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "$PS1" ]; then
if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then
# The file bash.bashrc already sets the default PS1.
# PS1='\h:\w\$ '
if [ -f /etc/bash.bashrc ]; then
. /etc/bash.bashrc
fi
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
export PATH=$PATH:/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin
非登录交互时: bash的配置顺序是:
/etc/bash.bashrc->~/.bashrc
我们可以看到不论是/etc/bash.bashrc还是~/.bashrc,都要求是交互模式才行:
5. 验证
登录交互: /etc/profile->/etc/bash.bashrc->~/.bash_profile->~/.bash_login->~/.profile->~/.bashrc
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$ shopt login_shell
login_shell on
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$ logout
liaojunwu@liaojunwu-virtual-machine:~$ exit
注销
liaojunwu@liaojunwu-virtual-machine:~$ exit
注销
liaojunwu@liaojunwu-virtual-machine:~$ exit
注销
liaojunwu@liaojunwu-virtual-machine:~$ su - root
密码:
[DEBUG]in /etc/profile!
[DEBUG]in /etc/bash.bashrc!
root@liaojunwu-virtual-machine:~#
root@liaojunwu-virtual-machine:~#
root@liaojunwu-virtual-machine:~# su - liaojunwu --login
[DEBUG]in /etc/profile!
[DEBUG]in /etc/bash.bashrc!
[DEBUG]in ~./profile!
[DEBUG]in ~./bashrc !
liaojunwu@liaojunwu-virtual-machine:~$
非登录交互: /etc/bash.bashrc->~/.bashrc
[DEBUG]in /etc/bash.bashrc!
[DEBUG]in ~./bashrc !
liaojunwu@liaojunwu-virtual-machine:~$ echo $-
himBH
liaojunwu@liaojunwu-virtual-machine:~$ shopt login_shell
login_shell off
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$
liaojunwu@liaojunwu-virtual-machine:~$ bash -i
[DEBUG]in /etc/bash.bashrc!
[DEBUG]in ~./bashrc !
liaojunwu@liaojunwu-virtual-machine:~$
6. PS1变量
参考博客: bash下变量PS1的完整理解_ps1变量_whowin的博客-CSDN博客