Shell 是 Linux 用来将用户输入的指令发送给内核的接口,并将内核执行指令后的结果返回输出。
这里使用 Bash shell 作为示例,shell 可以分成两类:Login Shell 和 Non Login Shell,两种类型分别执行不同的脚本来配置 shell 执行环境。
Login Shell
用户成功登陆后使用的是 Login shell。例如,当你通过终端、SSH 或使用 “su -” 命令来切换账号时都会使用 Login Shell。
Login shell 运行一系列的前置脚本来配置 shell 环境,运行如下命令可识别是否是 login shell。
$ echo $0
如果执行命令的结果类似 "-bash" 或 “-su”,那么你当前处于 login shell(确认有 “-” 前缀符号)
Login Shell 会执行如下的前置脚本:
- 执行文件 /etc/profile
- 执行 /etc/profile.d/ 目录下所有脚本
- 执行用户所属 ~/.bash_profile
- 执行 ~/.bashrc
- ~/.bashrc 执行文件 /etc/bashrc
Non Login Shell
Non Login Shell 是指通过 login shell 开启的 shell。例如,通过 shell 开启了一个新 shell 或者通过程序开启一个新 shell。
Non login shell 执行如下脚本来初始 shell 环境:
- 先执行 ~/.bashrc
- ~/.bashrc 会执行 /etc/bashrc
- /etc/bashrc 会调用 /etc/profile.d 中脚本
判断当前是 Login 还是 Non Login Shell
为了判断当前 shell 是 login 还是 nonlogin shell,可以简单的执行如下命令:
$ echo $0
Login shell 输出的结果类似 -bash 或 -su.
Non logins shell 输出的结果类似 bash 或 su
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。