Linux 登陆shell,交互shell以及环境变量读取顺序

简介: Linux用户在登陆到Linux服务器时,一些登陆的提示欢迎信息,以及特定的环境配置等等都按预先设定好的配置来生效。

Linux用户在登陆到Linux服务器时,一些登陆的提示欢迎信息,以及特定的环境配置等等都按预先设定好的配置来生效。Linux中的这个shell环境会读取很多不同的配置文件来达成上述目的,同时还有登陆shell与非登陆shell的区分。本文对此作简要描述,供大家参考!

一、登陆shell与非登陆shell

登陆shell(login shell):
  取得 bash 时需要完整的登陆流程的,就称为 login shell
  比如通过ssh方式连接,或者由tty1 ~ tty6 登陆,需要输入用户的账号与密码,此时取得的 bash 就称为login shell

非登陆shell(non-login shell):
  取得 bash 接口的方法不需要重复登陆的举动
  比如你以 X window 登陆 Linux 后, 再以 X 的图形化接口启动终端机,此时该终端接口无需输入账号与密码,则为non-login shell
  比如你在原本的 bash 环境下再次下达 bash 这个命令,同样的也没有输入账号密码, 那第二个 bash (子程序) 也是 non-login shell

查看登陆shell与非登陆shell

###演示环境
[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与非交互shell

交互式shell(interactive shell)
  交互式模式就是在终端上执行,shell等待你的输入,并且立即执行你提交的命令。这种模式被称作交互式是因为shell与用户进行交互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、退出。当你退出后,shell也终止了。

非交互式shell(non-interactive shell)
  shell也可以运行在另外一种模式:非交互式模式,以shell script(非交互)方式执行。在这种模式 下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾EOF,shell也就终止了。

###如下,执行 echo $-,查看其中的“i”选项(表示interactive shell)
[root@system1 ~]# echo $-
himBH

###如下,为非交互shell
[root@system1 ~]# echo 'echo $-' | bash
hB

三、登陆shell与非登陆shell调用的shell环境读取顺序

这里写图片描述
上图列出了登陆shell与非登陆shell读取的不同的shell环境配置文件。
其中,实线的的方向是主线流程,虚线的方向则是被调用(或读取)的配置文件
此外,对于登陆shell,读取~/.bash_profile配置文件时,会做出读取顺序判读,如下
  ~/.bash_profile —> ~/.bash_login —> ~/.profile
但 bash 的 login shell 配置只会读取上面三个文件的其中一个, 而读取的顺序则是依照上面的顺序。也就是说,如果 ~/.bash_profile 存在,那么其他两个文件不论有无存在,都不会被读取。 如果 ~/.bash_profile 不存在才会去读取 ~/.bash_login,而前两者都不存在才会读取 ~/.profile 的意思。

四、其他情形

1、除了读取上述配置文件之外,在登陆shell中还会读取其他相关配置信息,如读取 ~/.bash_history
2、对于shell环境变量修改之后需要立即生效的情形,可以使用source来立即生效。
用法
   # source 配置文件档名

###如修改了~/.bash_profile,不重新登陆希望生效的话,执行以下命令
# source ~/.bash_profile ###下一命令等价
# . ~/.bash_profile

3、shell登出
在shell登出是会读取 ~/.bash_logout

DBA牛鹏社(SQL/NOSQL/LINUX)

这里写图片描述

目录
相关文章
|
11天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
9天前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
12天前
|
Shell Linux
【Linux】12. 模拟实现shell
【Linux】12. 模拟实现shell
27 2
|
18天前
|
Shell Linux
Linux的shell入门教程shell脚本入门教程
Linux的shell入门教程shell脚本入门教程
15 0
|
25天前
|
存储 算法 Shell
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互(二)
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互
44 0
|
25天前
|
Shell Linux 开发工具
shell的介绍以及Linux权限的讲解
shell的介绍以及Linux权限的讲解
31 2
|
11天前
|
Web App开发 Linux 网络安全
工作中常用到的Linux命令
工作中常用到的Linux命令
|
8天前
|
NoSQL Linux Shell
常用的 Linux 命令
常用的 Linux 命令
30 9
|
1天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
9 0
|
2天前
|
域名解析 网络协议 Linux
Linux 中的 Nslookup 命令怎么使用?
【4月更文挑战第12天】
22 6
Linux 中的 Nslookup 命令怎么使用?