李守中

Bash 9 补充

Table of Contents

1 以 . 开头的 shell 配置文件的用途

  • ~/.bashrc 里存放的是和 bash 有关的东西
  • ~/.profile 里存放的是应该全局生效的东西 ( 比如,应当全局生效的环境变量 )
  • ~/.bash_profile 给 login shell 用,确保前两者都被加载 ( 里面有加载 .bashrc 和 .profile 的代码 )

2 Bash 配置文件的执行顺序

文字表达很复杂,直接上伪代码。

2.1 登入 shell ( interactive login shell )

登入 shell 是用户登入时创建的 shell。配置文件的执行顺序是:

execute /etc/profile
IF ~/.bash_profile exist THEN
    execute ~/.bash_profile
ELSE
    IF ~/.bash_login exist THEN
        execute ~/.bash_login
    ELSE
        IF ~/.profile exist THEN
            execute ~/.profile
        END IF
    END IF
END IF

2.2 非登入 shell ( interactive non-login shell )

用户登入以后会有一个 shell,如果此时在命令行执行 bash ( 在这个 shell 中创建一个新的子 shell ),那么这个新的子 shell 就是 非登入 shell 。子 shell 退出后会回到父 shell。

执行顺序:

IF ~/.bashrc exists THEN
    execute ~/.bashrc
END IF

2.3 用户登出

执行顺序:

IF ~/.bash_logout exists THEN
    execute ~/.bash_logout
END IF

2.4 不同发行版的细节

需要注意,在 RH 系发行版中 ~/.bashrc 会主动调用 /etc/bashrc:

# cat ~/.bashrc
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

3 快捷键绑定

Bash 的全局快捷键定义放在 /etc/inputrc 中。

用户可以把自己的快捷键定义放在 ~/.inputrc 中。同时也要在自定义文件中引用全局快捷键配置来避免遗漏。

$include /etc/inpurtc
...

具体写法可以查看文档: https://www.gnu.org/software/bash/manual/html_node/Readline-Init-File.html



Last Update: 2023-05-25 Thu 11:56

Contact: [email protected]     Generated by: Emacs 27.1 (Org mode 9.3)

若正文中无特殊说明,本站内容遵循: 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议