简单的查询:我刚刚意识到我从未见过脚本顶部的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>
。
最好考虑一下此功能,如下所示:
这个主题似乎bash
有时会出现,因此这里是一些各种调用方法及其结果的摘要。注意:为了帮助我添加了消息“ sourced $ HOME / .bashrc”和“ sourced” $ HOME / .bash_profile”添加到它们各自的文件中。
基本电话
bash -i
$ bash -i
sourced /home/saml/.bashrc
bash -l
$ bash -l
sourced /home/saml/.bashrc
sourced /home/saml/.bash_profile
bash -il-或-bash -li
$ bash -il
sourced /home/saml/.bashrc
sourced /home/saml/.bash_profile
bash -c“ ..cmd ..”
$ bash -c 'echo hi'
hi
注意:请注意,该-c
开关未提供任何一个文件!
禁止读取配置文件
bash --norc
$ bash --norc
bash-4.1$
bash --noprofile
$ bash --noprofile
sourced /home/saml/.bashrc
bash --norc -i
$ bash --norc -i
bash-4.1$
bash --norc -l
$ bash --norc -l
sourced /home/saml/.bashrc
sourced /home/saml/.bash_profile
bash --noprofile -i
$ bash --noprofile -i
sourced /home/saml/.bashrc
bash --noprofile -l
$ bash --noprofile -l
bash-4.1$
bash --norc -i-或-bash --norc -l
$ bash --norc -c 'echo hi'
hi
更深奥的bash调用方式
bash --rcfile $ HOME / .bashrc
$ bash -rcfile ~/.bashrc
sourced /home/saml/.bashrc
bash --norc --rcfile $ HOME / .bashrc
$ bash --norc -rcfile ~/.bashrc
bash-4.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
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] 删除。
我来说两句