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

水陆两用

简单的查询:我刚刚意识到我从未见过脚本顶部的shebang.bashrc,这使我认为系统在登录(${SHELL}时使用默认的shell来提供源代码我正在考虑这种情况的原因,即使用默认外壳程序以外的其他东西来运行登录脚本是否被视为一种坏习惯。

你好

.bashrc.bash_profile不是脚本。它们是每次使用bash以下两种方式之一执行的配置文件

  • 交互的
  • 登录

bash手册页INVOCATION部分是相关的。

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

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

以下段落描述了如何bash执行其启动文件。如果存在任何文件但无法读取,则bash报告错误。如以下扩展”部分中的“ Tilde扩展”下所述,Tildes的文件名扩展

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

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

启动不是登录外壳程序交互式外壳程序时~/.bashrc,如果该文件存在,bash将从中读取并执行命令使用--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
    
  2. bash -l

    $ bash -l
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    
  3. bash -il-或-bash -li

    $ bash -il
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    
  4. bash -c“ ..cmd ..”

    $ bash -c 'echo hi'
    hi
    

    注意:请注意,该-c开关未提供任何一个文件!

禁止读取配置文件

  1. bash --norc

    $ bash --norc
    bash-4.1$ 
    
  2. bash --noprofile

    $ bash --noprofile
    sourced /home/saml/.bashrc
    
  3. bash --norc -i

    $ bash --norc -i
    bash-4.1$ 
    
  4. bash --norc -l

    $ bash --norc -l
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    
  5. bash --noprofile -i

    $ bash --noprofile -i
    sourced /home/saml/.bashrc
    
  6. bash --noprofile -l

    $ bash --noprofile -l
    bash-4.1$ 
    
  7. bash --norc -i-或-bash --norc -l

    $ bash --norc -c 'echo hi'
    hi
    

更深奥的bash调用方式

  1. bash --rcfile $ HOME / .bashrc

    $ bash -rcfile ~/.bashrc 
    sourced /home/saml/.bashrc
    
  2. bash --norc --rcfile $ HOME / .bashrc

    $ bash --norc -rcfile ~/.bashrc 
    bash-4.1$ 
    

这些失败了

  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
    
  2. bash -i -rcfile .bashrc

    $ bash -i -rcfile .bashrc
    sourced /home/saml/.bashrc
    sourced /home/saml/.bash_profile
    bash: .bashrc: command not found
    

可能还有更多,但希望您能明白这一点。

还有什么?

最后,如果您对这个主题非常感兴趣,以至于想阅读/探索更多,我强烈建议您阅读《 Bash初学者指南》,特别是1.2伯恩的优势再次凸显在该小节下的各个小节中,“ 1.2.2.1。调用”“ 1.2.2.3.3。交互式外壳行为”解释了可以调用的各种方法之间的低级差异bash

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么suse的root用户没有.bash_profile或.bashrc

为什么$ GOPATH通常设置在〜/ .bashrc和〜/ .bash_profile中而不是〜/ .profile中?

〜/ .profile,〜/ .bashrc,〜/ .bash_profile,〜/ .gnomerc,/ etc / bash_bashrc,/ etc / screenrc ...之间有什么区别?

为什么Ubuntu默认的〜/ .profile源〜/ .bashrc?

人们为什么从bashrc而不是反过来获取bash_profile?

为什么Mac iterm和iterm2不提供.bashrc或.bash_profile?

为什么我的.bashrc有错误?

为什么sh(不是bash)抱怨.bashrc中定义的函数?

为什么〜/ .config或〜/ .bashrc被视为bash中的目录?

是bashrc还是bash_profile?

为什么即使使用bash启动终端仅执行〜/ .bashrc,在〜/ .profile中仍定义PATH?

为什么rsync读取.bashrc而不读取.profile?

为什么不从.profile读取我的.bashrc文件

为什么.profile(而不是.bashrc)将〜/ bin附加到路径?

从终端(fedora)以root用户登录时,为什么要更改用户?为什么我对.bashrc和.bash_profile所做的更改不显示?

为什么“ ls * bash *”不显示.bashrc文件

'〜/ .bashrc'与'/etc/skel/.bashrc'。为什么有两个“ .bashrcs”?

~/.profile 和 ~/.bashrc 中有什么内容?

对于未从bash启动的应用程序,Ubuntu为什么不从我的.bashrc / .profile加载设置?

.bashrc中的“ rc”代表什么?

.profile和.bash_profile有什么区别?为什么我的系统上没有.profile文件?

bash_profile或bashrc中的环境变量?

.bashrc / .bash_profile未加载导出

.bashrc和.bash_profile之间的区别

为什么要在〜/ .bash_profile中添加〜/ .profile?

为什么没有这样的非交互式版本的bashrc?

为什么/etc/bash.bashrc中的别名不适用于sudo?

有什么作用。.bashrc实际做什么?

/ etc / profile和.bashrc有什么区别