非登录交互式 shell(例如:当我在 Ubuntu 中打开终端窗口时)加载并执行文件~/.bashrc
.
现在,当我执行 shell 脚本时,将创建一个非交互式非登录 shell 进程来运行此 shell 脚本。
是否有一个文件被这个非交互式非登录 shell 加载并执行?
编辑:我使用的外壳是bash
.
答案1
从man bash
, 在一个非交互式shellBASH_ENV
会评估环境变量并获取其内容。因此,您可以使用该环境变量来指定具有环境设置的文件。
默认情况下不会来源任何文件非交互式模式。
When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV
in the environment, expands its value if it appears there, and uses the expanded value as the name of a file
to read and execute. Bash behaves as if the following command were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the filename.
答案2
当您使用非交互式非登录 shell 执行某些 shell 脚本时,特别是使用系统(3)在一些 C 程序中(或者通过运行它定时任务(5)),没有文件被隐式加载。
.
您的脚本可能会使用或获取某些文件source
外壳内置。
也许你应该关心哪个环境(参见环境(7)) 或者是什么文件描述符是相关的,这取决于你的 shell 脚本是如何启动的,祖先进程是什么,等等。如果你从终端窗口中的 shell 运行你的脚本,它会从你的 shell 继承很多东西(特别是你的 shell)。$PATH
)。但是,如果您以不同方式运行该脚本(在某些 init 或 systemd 脚本中,通过cron
或batch
或nohup
, ....),情况可能会有所不同。
读叉子(2),执行(2)(两者都在您的 shell 中使用),凭证(7)
阅读有关章节Bash 启动文件。