为什么 .bashrc/.bash_profile 中没有 shebang?

amp*_*ent 27 shell profile bashrc shebang

简单查询:我刚刚意识到我从未在脚本顶部看到过shebang.bashrc,这让我认为系统在登录时使用默认shell 来获取它(${SHELL})。我正在思考为什么会出现这种情况,即使用默认 shell 以外的其他东西来运行登录脚本是否被认为是一个坏习惯。

slm*_*slm 34

.bashrc.bash_profile不是脚本。它们是每次bash以两种方式之一执行时获取的配置文件:

  • 交互的
  • 登录

bash 手册页的调用部分是相关的。

一个登录shell是一个其参数零的第一个字符是一个-,或者启动时的--login选项。

一个交互式壳是一个启动时没有非选项参数,并且没有将-c其标准输入和错误都被连接到终端(如通过选项isatty(3)),或开始与一个-i 选项。PS1被设置并且 $-包括i如果bash是交互式的,允许外壳脚本或启动文件来测试此状态。

以下段落描述如何bash执行其启动文件。如果任何文件存在但无法读取,bash 将报告错误。如下所述波浪线在文件名扩展 波浪线扩展扩展 部分。

当 bash 作为交互式登录 shell 或作为具有--login选项的非交互式 shell调用时,它首先从文件中读取并执行命令 /etc/profile(如果该文件存在)。读取该文件后,它会按该顺序查找~/.bash_profile~/.bash_login、 和 ~/.profile,并从第一个存在且可读的命令中读取和执行命令。--noprofile当 shell 启动时可以使用该 选项来禁止这种行为。

当登录 shell 退出时,bash 从文件中读取并执行命令~/.bash_logout(如果存在)。

当一个不是登录 shell的交互式shell 启动时,bash 读取并执行来自 的命令~/.bashrc(如果该文件存在)。这可以通过使用--norc选项来禁止。该--rcfile file 选项将强制 bash 从文件而不是~/.bashrc.

您可以控制何时通过命令行开关加载它们,--norc并且--noprofile. 您还可以覆盖使用--rcfile开关加载它们的位置。

正如其他人所提到的,您可以模拟如何通过使用source <file>命令或使用命令来加载这些文件. <file>

最好按如下方式考虑此功能:

  1. bash 以裸环境启动
  2. bash 然后打开这些文件之一(取决于它是如何作为交互式或登录调用的,然后......
  3. ...逐行执行文件中的每个命令...
  4. 当完成以提示的形式给出控制权时,等待输入

调用方法

这个话题似乎每隔一段时间就会出现,所以这里有一个关于各种调用方式bash及其结果的小抄。注意:为了帮助我添加了消息“sourced $HOME/.bashrc”和“sourced $HOME/.bash_profile" 到它们各自的文件。

基本调用

  1. bash -i

    $ bash -i
    sourced /home/saml/.bashrc
    
    Run Code Online (Sandbox Code Playgroud)
  2. bash -l

    $ bash -l
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    
    Run Code Online (Sandbox Code Playgroud)
  3. bash -il -或- bash -li

    $ bash -il
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    
    Run Code Online (Sandbox Code Playgroud)
  4. bash -c "..cmd.."

    $ bash -c 'echo hi'
    hi
    
    Run Code Online (Sandbox Code Playgroud)

    注意:请注意,-c交换机没有提供任何文件!

禁止读取配置文件

  1. bash --norc

    $ bash --norc
    bash-4.1$ 
    
    Run Code Online (Sandbox Code Playgroud)
  2. bash --noprofile

    $ bash --noprofile
    sourced /home/saml/.bashrc
    
    Run Code Online (Sandbox Code Playgroud)
  3. bash --norc -i

    $ bash --norc -i
    bash-4.1$ 
    
    Run Code Online (Sandbox Code Playgroud)
  4. bash --norc -l

    $ bash --norc -l
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    
    Run Code Online (Sandbox Code Playgroud)
  5. bash --noprofile -i

    $ bash --noprofile -i
    sourced /home/saml/.bashrc
    
    Run Code Online (Sandbox Code Playgroud)
  6. bash --noprofile -l

    $ bash --noprofile -l
    bash-4.1$ 
    
    Run Code Online (Sandbox Code Playgroud)
  7. bash --norc -i -or- bash --norc -l

    $ bash --norc -c 'echo hi'
    hi
    
    Run Code Online (Sandbox Code Playgroud)

调用 bash 的更深奥的方法

  1. bash --rcfile $HOME/.bashrc

    $ bash -rcfile ~/.bashrc 
    sourced /home/saml/.bashrc
    
    Run Code Online (Sandbox Code Playgroud)
  2. bash --norc --rcfile $HOME/.bashrc

    $ bash --norc -rcfile ~/.bashrc 
    bash-4.1$ 
    
    Run Code Online (Sandbox Code Playgroud)

这些失败

  1. bash -i -rcfile ~/.bashrc

    $ bash -i -rcfile ~/.bashrc 
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    bash: /home/saml/.bashrc: restricted: cannot specify `/' in command names
    
    Run Code Online (Sandbox Code Playgroud)
  2. bash -i -rcfile .bashrc

    $ bash -i -rcfile .bashrc
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    bash: .bashrc: command not found
    
    Run Code Online (Sandbox Code Playgroud)

可能还有更多,但你明白了,希望......

还有什么?

最后,如果您对这个主题如此着迷,以至于想要阅读/探索更多内容,我强烈建议您查看 Bash 初学者指南,特别是第1.2Bourne Again SHell 的优势“1.2.2.1. 调用”“1.2.2.3.3. 交互式外壳行为”下的各个小节解释了您可以调用的各种方式之间的低级差异bash


ric*_*ici 14

.bashrc脚本只能bash自己运行。它们不是独立的,也不打算被exec系统使用。(实际上,它们通常没有标记为可执行文件,而且,正如您所说,它们没有 shebang 行。)

此类脚本旨在成为sourced,因为它们通常会执行诸如更改环境变量($PATH例如)之类的事情,这些变量预计会在脚本完成后持续存在。所以尝试在子shell中执行一个真的毫无意义。


jll*_*gre 6

除了其他回复,请注意,如果您需要,没有什么禁止您在这些配置文件的开头放置shebang。

这不会损害 shell 采购它们,因为 shebang 将像常规评论一样被处理,即被忽略。

这可能有助于使用语法突出显示的编辑器找出文件中使用的编程语言。

请注意,一些编辑器喜欢vim为后者提供替代方法,例如模式行。也就是说,你可以随时把模式在线条的结束~/.bashrc~/.bash_profile像这样:

...
<code in ~/.bashrc>
...
# vim: ft=sh :
Run Code Online (Sandbox Code Playgroud)

  • 上面@slm 接受的答案很好,但这就是我一直在寻找的,关于根据 ShellCheck 的建议在我的 `.bash_profile` 开头添加一个shebang。 (2认同)