Linux用户在登录到Linux服务器时,一些登录的提示欢迎信息,以及特定的环境配置等等都按预先设定好的配置来生效。Linux中的这个shell环境会读取不少不一样的配置文件来达成上述目的,同时还有登录shell与非登录shell的区分。本文对此做简要描述,供你们参考!shell
登录shell(login shell):
取得 bash 时须要完整的登录流程的,就称为 login shell
好比经过ssh方式链接,或者由tty1 ~ tty6 登录,须要输入用户的帐号与密码,此时取得的 bash 就称为login shellwindows
非登录shell(non-login shell):
取得 bash 接口的方法不须要重复登录的举动
好比你以 X window 登录 Linux 后, 再以 X 的图形化接口启动终端机,此时该终端接口无需输入帐号与密码,则为non-login shell
好比你在本来的 bash 环境下再次下达 bash 这个命令,一样的也没有输入帐号密码, 那第二个 bash (子程序) 也是 non-login shellruby
查看登录shell与非登录shellbash
###演示环境 [root@system1 ~]# more /etc/redhat-release Red Hat Enterprise Linux Server release 7.0 (Maipo) ###当前从ssh登录到服务器 [root@system1 ~]# tty /dev/pts/1 # ### Author : Leshami QQ/Weixin : 645746311 # ### Blog : http://blog.csdn.net/leshami ###输入 echo $0, 显示结果为 -bash ,即为登录shell [root@system1 ~]# echo $0 -bash [root@system1 ~]# ps PID TTY TIME CMD 77122 pts/1 00:00:00 bash 77157 pts/1 00:00:00 ps ###下面在X windows打开一个终端,以下,显示为/bin/bash,即非登录shell [root@system1 Desktop]# echo $0 /bin/bash [root@system1 ~]# ps -ef|grep pts|grep bash root 73245 73241 0 11:49 pts/0 00:00:00 /bin/bash root 76511 73245 0 16:19 pts/0 00:00:00 bash root 77122 77118 0 17:02 pts/1 00:00:00 -bash root 77158 77118 0 17:03 pts/2 00:00:00 -bash root 77210 73241 0 17:04 pts/3 00:00:00 /bin/bash root 77283 77279 0 17:06 pts/4 00:00:00 -bash root 77332 77122 0 17:06 pts/1 00:00:00 grep --color=auto bash ###在上传的结果中73245,77210为非登录shell,77122,77158,77283为登录shell
交互式shell(interactive shell)
交互式模式就是在终端上执行,shell等待你的输入,而且当即执行你提交的命令。这种模式被称做交互式是由于shell与用户进行交互。这种模式也是大多数用户很是熟悉的:登陆、执行一些命令、退出。当你退出后,shell也终止了。服务器
非交互式shell(non-interactive shell)
shell也能够运行在另一种模式:非交互式模式,以shell script(非交互)方式执行。在这种模式 下,shell不与你进行交互,而是读取存放在文件中的命令,而且执行它们。当它读到文件的结尾EOF,shell也就终止了。ssh
###以下,执行 echo $-,查看其中的“i”选项(表示interactive shell) [root@system1 ~]# echo $- himBH ###以下,为非交互shell [root@system1 ~]# echo 'echo $-' | bash hB
上图列出了登录shell与非登录shell读取的不一样的shell环境配置文件。
其中,实线的的方向是主线流程,虚线的方向则是被调用(或读取)的配置文件
此外,对于登录shell,读取~/.bash_profile配置文件时,会作出读取顺序判读,以下
~/.bash_profile —> ~/.bash_login —> ~/.profile
但 bash 的 login shell 配置只会读取上面三个文件的其中一个, 而读取的顺序则是依照上面的顺序。也就是说,若是 ~/.bash_profile 存在,那么其余两个文件不论有无存在,都不会被读取。 若是 ~/.bash_profile 不存在才会去读取 ~/.bash_login,而前二者都不存在才会读取 ~/.profile 的意思。spa
一、除了读取上述配置文件以外,在登录shell中还会读取其余相关配置信息,如读取 ~/.bash_history
二、对于shell环境变量修改以后须要当即生效的情形,可使用source来当即生效。
用法
# source 配置文件档名.net
###如修改了~/.bash_profile,不从新登录但愿生效的话,执行如下命令
# source ~/.bash_profile ###下一命令等价
# . ~/.bash_profile3d
三、shell登出
在shell登出是会读取 ~/.bash_logoutcode