站内链接:

Glossary

login

  • ```login shell:获取 bash 时需要完整的登录流程,需要输入user/passwd`,例如终端 2, 3, 4 等等
  • non-login shell:使用 xwindows 登录 linux 之后,重新开启 bash,此时是 non-login 登录方式

登录: -l 表示 login 登录, 否则表示 non-login 登录

参数

在讲解 bash, zsh 等终端环境之前先来看一下一些常用的 bash 命令参数, 这些参数使用频率非常高

  1. -c: 那么命令将从 string 中读取。如果 string 后面有参数, 它们将用于给位置参数赋值
    这是极为常见的用法, 例如 supervisorctl 配置场景, 远程执行命令场景中, 通过该选项避免了某些特殊字符无法被识别或者识别错误的问题:
1
2
3
$SHELL -l -c "echo abc >/dev/null 2>&1"
# 打印-1退出码
bash -c "exit -1; echo $?"
  1. -l: 使得 bash 以类似登录 shell, 这个在下面介绍四种登录模式的时候会说明, 该选项非常有用, 其确保在登录方式下可以自动加载默认的配置文件从而确保 bash 执行上下文正常.

Bash

登录启动脚本

所有用户:

  • /etc/profile——为系统的每一个用户设置 env,用户首次登陆,该文件执行,并获取/etc/profile.d 目录下的配置
  • /etc/bashrc——为每一个运行 bash shell 的用户执行该文件,注意,任意打开一个 bash 都会执行
  • ubuntu 中不存在/etc/bashrc,替换文件为/etc/bash.bashrc

单一用户:

  • .bash_profile——用户首次登陆时执行,用于设置 shell 环境,centos 中会默认执行并调用.bashrc
  • .bashrc——每次打开 shell 时执行该文件
  • .bash_logout——每次退出系统,bash shell 时执行

登录

login shell 登录:

  • 存在.bash_profile, 读取并运行
  • 存在.bash_log, 读取并运行
  • 存在.profile, 读取并运行

non-login shell 登录:.bashrc 文件

登出

login 方式登录,此时登出后,执行.bash_logout 文件

配色方案

.bash_profile 和.bashrc 文件会为启动的终端加载相应的系统配色方案,
尤其是 ubuntu 中的.bashrc,保证尽量加载,否则会出现所有文件都是无颜色状态.

例子

1
2
3
4
5
6
7
8
# 图形模式登录时, 读取顺序
/etc/profile -> ~/.profile

# 图形模式登录后,打开终端
/etc/bash.bashrc -> ~/.bashrc

# 文本模式登录时
/etc/bash.bashrc -> /etc/profile -> ~/.bash_profile

zshrc

可用 shell

cat /etc/shells

glossary

具体信息, 见zshrc

参考