李守中

Bash 9 补充

Table of Contents

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

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

2 单引号和双引号的区别

在一对双引号内使用单引号,则单引号会被剥夺引号的语义变成普通字符。同样地,在一对单引号内使用双引号,双引号也会被剥夺引号的语义变成普通字符。

双引号会将 dollar 符 $ 和反引号 ` 分别按 bash 中的特殊含义处理,即遇到 $ 就开始找变量名,遇到 ` 就开始找下一个反引号。

而在单引号内使用的 dollar 符 $ 和反引号 ` 会按普通字符处理。

lsz@debian11:~$ num=1000
lsz@debian11:~$ echo "$num `echo ee`"
1000 ee
lsz@debian11:~$ echo '$num `echo ee`'
$num `echo ee`

3 Bash 配置文件的执行顺序

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

3.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

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

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

执行顺序:

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

3.3 用户登出

执行顺序:

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

3.4 不同发行版的细节

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

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

4 快捷键绑定

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

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

$include /etc/inpurtc
...

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



Last Update: 2023-08-30 Wed 14:48

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

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