交互式shell和非交互式shell、登陆shell和非登陆shell的区别

交互式shell和非交互式shell、登陆shell和非登陆shell的区别。
首先,这是两个不一样的维度来划分的,一个是是否交互式,另外一个是是否登陆。linux

交互式shell和非交互式shell(interactive shell and non-interactive shell)
交互式模式就是在终端上执行,shell等待你的输入,而且当即执行你提交的命令。这种模式被称做交互式是由于shell与用户进行交互。这种模式也是大多数用户很是熟悉的:登陆、执行一些命令、退出。当你退出后,shell也终止了。
shell也能够运行在另一种模式:非交互式模式,以shell script(非交互)方式执行。在这种模式 下,shell不与你进行交互,而是读取存放在文件中的命令,而且执行它们。当它读到文件的结尾EOF,shell也就终止了。
能够经过打印“$-”变量的值(表明着当前shell的选项标志),查看其中的“i”选项(表示interactive shell)来区分交互式与非交互式shell。shell

 
yang@mint-linux ~ $ echo $-
himBH
yang@mint-linux ~ $ cat test.sh
echo $-
yang@mint-linux ~ $ ./test.sh 
hB
yang@mint-linux ~ $ 

登陆shell和非登陆shell
登陆shell:是须要用户名、密码登陆后才能进入的shell(或者经过--login”选项生成的shell)。
非登陆shell:固然就不须要输入用户名和密码便可打开的Shell,例如:直接命令“bash”就是打开一个新的非登陆shell,在Gnome或KDE中打开一个“终端”(terminal)窗口程序也是一个非登陆shell。
执行exit命令,退出一个shell(登陆或非登陆shell);
执行logout命令,退出登陆shell(不能退出非登陆shell)。
bash

yang@mint-linux ~ $ su yang --login
Password: 
Hello from .bash_profile
yang@mint-linux ~ $ exit
logout
Hello from .bash_logout
yang@mint-linux ~ $ su yang --login
Password: 
Hello from .bash_profile
yang@mint-linux ~ $ logout
Hello from .bash_logout
yang@mint-linux ~ $ su yang
Password: 
Hello from .bashrc
yang@mint-linux ~ $ exit
exit
yang@mint-linux ~ $ su yang
Password: 
Hello from .bashrc
yang@mint-linux ~ $ logout
bash: logout: not login shell: use `exit' yang@mint-linux ~ $ 

对于Bash来讲,登陆shell(包括tty1~tty6登陆shell和使用“--login”选项的交互shell),它会首先读取和执行/etc/profile全局配置文件中的命令,而后依次查找~/.bash_profile、~/.bash_login 和 ~/.profile这三个配置文件,读取和执行这三个中的第一个存在且可读的文件中命令。
在非登陆shell里,只读取 ~/.bashrc (和 /etc/bash.bashrc、/etc/bashrc )文件,不一样的发行版里面可能有所不一样。post

 其中Ubuntu中~/.profile中包含spa

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

转载:http://smilejay.com/2012/10/interactive-shell-login-shell/code

相关文章
相关标签/搜索