Answers:
.bashrc和.bash_profile是不是脚本。它们是每次使用bash以下两种方式之一获取的配置文件:
bash手册页的INVOCATION部分是相关的。
一个登录shell是一个其参数零的第一个字符是一个
-,或者启动时的--login选项。一个交互式壳是一个启动时没有非选项参数,并且没有将
-c其标准输入和错误都被连接到终端(如通过选项isatty(3)),或开始与一个-i选项。PS1被设置并且$-包括i如果bash是交互式的,允许外壳脚本或用于测试此状态的启动文件。以下段落描述了如何
bash执行其启动文件。如果存在任何文件但无法读取,则bash报告错误。如下所述波浪线在文件名扩展 波浪线扩展在扩展 部分。当bash作为交互式登录shell或带有
--login选项的非交互式shell 被调用时,它首先从文件/etc/profile(如果该文件存在)中读取并执行命令 。读取文件后,它会查找~/.bash_profile,~/.bash_login以及~/.profile以该顺序,并读取并从存在并且可读的第一个执行命令。--noprofile启动外壳程序以禁止此行为时,可以使用该 选项。当退出登录shell时,bash从文件中读取并执行命令
~/.bash_logout(如果存在)。启动不是登录外壳程序的交互式外壳程序时,bash会从中读取并执行命令
~/.bashrc,如果该文件存在的话。使用--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_profilebash --noprofile -i
$ bash --noprofile -i
sourced /home/saml/.bashrcbash --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/.bashrcbash --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 namesbash -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。交互式shell行为”解释了可以调用的各种方法之间的低级差异bash。
.bash_profile包括了一条源代码.bashrc。但我认为这是非常典型的设置。
.bashrc而.bash_profile不是脚本。恕我直言,它们是特定目的的脚本,在bash初始化期间隐式地来源,或者在您需要应用其修改时显式地来源。它们不只是按照配置文件中的预期配置bash环境(变量,函数,别名...)。他们可以执行常见脚本中的任何操作。例如,他们可以启动各种操作,例如后台任务,编写日志记录,初始化某些程序等。无论如何,感谢您提供详细的摘要!
除了其他答复外,请注意,如果需要,也不能禁止在这些配置文件的开头放上空格。
这不会损害shell采购它们,因为shebang的处理将像常规注释一样,即被忽略。
这可能会帮助使用语法突出显示的编辑器找出文件中使用的编程语言。
请注意,有些编辑器vim提供了替代方式,例如为后者提供模式行。也就是说,你可以随时把模式在线条的结束~/.bashrc而~/.bash_profile像这样:
...
<code in ~/.bashrc>
...
# vim: ft=sh :
.bash_profile在ShellCheck的推荐下在我的开头添加一个Shebang。
我在任何地方都读不知道确切的地方,但这是真的
Bash手册在这方面有些混乱,但是Bash并不像shell脚本那样执行〜/ .bash_profile。它会读取文件,然后执行其中的命令(您可以通过运行source〜/ .bash_profile来执行类似的操作)。