Bash 配置文件详解

这是我写的第二篇博客,如果有错误的地方,请各位积极指正。

Bash 简介

Bash(Bourne Again shell)是一种与 sh 兼容的命令语言解释器,它执行从标准输入或文件读取的命令。Bash 还结合了 Korn 和 C shell(ksh 和 csh)的有用功能。

很多 Linux 发行版都将 Bash 作为默认的 shell 解释器和 CLI,因此 Bash 是现如今最为通用的一种shell解释器。

login shell 和 non-login shell

  • login shell:一般理解为是在用户登录系统时启动的 shell。比如通过 SSH 登录服务器时会启动一个 login shell;登录没有桌面环境的 Linux 系统时会启动一个 login shell。更精确地来说,在 Bash 的 man page 中,login shell 被解释为:A login shell is one whose first character of argument zero is a -, or one started with the --login option。也就是说 bash --login 可以启动 login shell
  • non-login shell:不是 login shell 的 shell 就是 non-login shell。比如,桌面版调出的终端;直接执行 bash 命令等。

interactive shell 和 non-interactive shell

  • interactive shell:一般理解为是一种可交互的shell,我们看到的常见的终端程序就是一种交互式的shell。在 Bash 的 man page 中这么定义它:An interactive shell is one started without non-option arguments (unless -s is specified) and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. PS1 is set and $- includes i if bash is interactive, allowing a shell script or a startup file to test this state。
  • non-interactive shell:不可以交互的shell。比如执行脚本的子shell就是一种 non-interactive shell 。

注意: 以下的 shell 默认都是 interactive 的,对 non-interactive 感兴趣的朋友可以使用 man bash 自行查看。

Bash 配置文件

测试的 Bash 版本为 5.1.16。

Bash 的配置文件主要为:

  • /etc/profile
  • /etc/profile.d/*.sh
  • /etc/bash.bashrc
  • ~/.bash_profile, ~/.bash_login or ~/.profile
  • ~/.bashrc
  • ~/.bash_logout
  • ~./bash_history

其中,根目录下的是系统级的配置文件,home 目录下的是用户级的配置。

下面是读取和执行流程。

对于 login shell 和 non-login shell,它们都会读取和执行 ~/.bash_history,这是一个记录 bash 历史记录的文件,bash 会对之前所有命令进行记录并写入这个文件,配合history 命令可以很好的查询和执行历史命令。

上半部分是 login,下半部分是 non-login。其中 auto 是 bash 自身的读取和执行行为,而 manually 指示读取和执行行为是该文件中的脚本所引起的。

login shell

对于 login shell,它首先读取和执行 /etc/profile,之后按顺序读取和执行 ~/.bash_profile, ~/.bash_login 或 ~/.profile 中的任意一个(比如,如果存在 ~/.bash_profile 就会忽略后面两个;如果不存在 ~/.bash_profile,但是存在 ~/.bash_login,就会忽略最后一个)。其余文件的读取和执行是文件中的 shell script 指定的,我姑且把它叫做 “手动地”。比如,一般 /etc/profile 的内容如下:

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
 # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
 
 if [ "${PS1-}" ]; then
   if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then
     # The file bash.bashrc already sets the default PS1.
     # PS1='\h:\w\$ '
     if [ -f /etc/bash.bashrc ]; then
       . /etc/bash.bashrc
     fi
   else
     if [ "$(id -u)" -eq 0 ]; then
       PS1='# '
     else
       PS1='$ '
     fi
   fi
 fi
 
 if [ -d /etc/profile.d ]; then
   for i in /etc/profile.d/*.sh; do
     if [ -r $i ]; then
       . $i
     fi
   done
   unset i
 fi

​可以很明显地看出 /etc/profile 在当前 shell 环境中加载和执行了 /etc/bash.bashrc 和 /etc/profile.d/*.sh。

当 login shell 退出登录时,读取和执行 ~./bash_logout 。

non-login shell

对于 non-login shell,bash 读取和执行 /etc/bash.bashrc 和 ~/.bashrc。

其它

建议

所以,当进行配置时,一般都写入 ~/.bashrc 中。当然,也可以在 ~/.bashrc 读取和执行其他脚本。

可能的疑问

如果你们熟悉你们的 Linux 发行版,可能会发现一个问题:终端一般都是 non-login shell,但是你会发现你的 non-login shell 中 出现了 login shell 才有的环境变量。这是为什么?

其实原因很简单,在登录桌面环境时,系统会使用 non-interactive login shell 进行一些初始化,non-interactive login shell 和 interactive login shell 一样,也会读取和执行 /etc/profile 和 /etc/profile.d/*.sh,再调出 non-login shell 就会继承这些初始化的配置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值