[英]How can I get the list of all files that are sourced by bash?
Is there a way to find out all the files that are sourced by bash?有没有办法找出所有由 bash 提供的文件?
Alternately, is there a single point of entry (or a first point of entry) where I can go to follow and find this information by adding a set -x
at the top?或者,是否有一个单一的入口点(或第一个入口点),我可以通过在顶部添加set -x
来跟踪和查找此信息?
(By single point of entry, I do not mean ~/.bashrc or ~/.bash_profile because some other file higher in the source chain tells bash to load these above files in the first place). (通过单点入口,我的意思不是 ~/.bashrc 或 ~/.bash_profile 因为源链中更高的一些其他文件告诉 bash 首先加载这些文件)。
There is no easy catch-all answer here, it depends on the combination of login/interactive attributes.这里没有简单的包罗万象的答案,这取决于登录/交互属性的组合。
A login shell will source /etc/profile
, and only the first one it finds among ~/.bash_profile
, ~/.bash_login
, and ~/.profile
.登录 shell 将获取/etc/profile
,并且只有它在~/.bash_profile
、 ~/.bash_login
和~/.profile
找到的第一个。 You could call these independent points of entry: /etc/profile
doesn't need to explicitly source one of the others, it's bash
that does it.您可以调用这些独立的入口点: /etc/profile
不需要明确地获取其他入口之一,它是bash
来做的。
For non-login interactive, you have /etc/bash.bashrc
and ~/.bashrc
, again independent.对于非登录交互,你有/etc/bash.bashrc
和~/.bashrc
,再次独立。
For non-login non-interactive, the single point of entry is $BASH_ENV
, if defined.对于非登录非交互式,单点入口是$BASH_ENV
,如果已定义。
You can find the official description at the GNU Bash manual under Bash startup files .您可以在 GNU Bash 手册中的Bash 启动文件下找到官方说明。
There are several places where the process to load all startup files start.加载所有启动文件的过程从多个位置开始。
The table in this link will make it clear: 此链接中的表格将清楚说明:
Interactive login /etc/profile
Interactive non-login /etc/bash.bashrc
Script $BASH_ENV
Understanding login
as either an Interactive login or a non-interactive shell called with the option --login.将login
理解为交互式登录或使用选项 --login 调用的非交互式 shell。 From man bash
:从man bash
:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the
--login
option, it first reads and executes commands from the file/etc/profile
, if that file exists.当 bash 作为交互式登录 shell 或作为具有--login
选项的非交互式 shell 调用时,它首先从文件/etc/profile
读取并执行命令(如果该文件存在)。
However, the most common call to an interactive shell is su - $USER
which use a -
as the first character of the command called (not --login
).但是,对交互式 shell 的最常见调用是su - $USER
,它使用-
作为所调用命令的第一个字符(不是--login
)。
That is the default.这是默认设置。 Nothing prevent you from editing those files and add in /etc/profile something like:没有什么可以阻止您编辑这些文件并在 /etc/profile 中添加以下内容:
if [ -f /etc/bash.bashrc ]; then
source /etc/bash.bashrc
fi
Which will ensure that /etc/bash.bashrc
would be sourced in all cases of Interactive
shells.这将确保/etc/bash.bashrc
在Interactive
shell 的所有情况下都是来源。
Care should be taken to avoid duplicating variables or actions (sourced in both files).应注意避免重复变量或操作(来自两个文件)。 Defining a variable and checking that it has been already set before some actions would make this process more reliable.在某些操作之前定义一个变量并检查它是否已经设置会使这个过程更可靠。
The starting point for scripts, from the point of view of bash, is the variable $BASH_ENV
which has to be set in the environment before bash is called.从 bash 的角度来看,脚本的起点是变量$BASH_ENV
,它必须在调用 bash之前在环境中设置。 That expands the search to other shells or programs that may call bash.这将搜索扩展到其他可能调用 bash 的 shell 或程序。 There is no single definitive solution in this case, only what is the usual practice.在这种情况下没有单一的确定解决方案,只有通常的做法。
The usual practice is to not use $BASH_ENV at all, so bash would start with all the compiled-in options only.通常的做法是根本不使用 $BASH_ENV,因此 bash 将仅从所有编译选项开始。
Reviving this question because there is an automation for this:重温这个问题,因为有一个自动化:
Execute bash and carve it out of the output.执行 bash 并将其从输出中剔除。 -li
is login interactively, -x
prints out what bash is doing internally, and -c exit
just tells bash to terminate immediately. -li
是交互式登录, -x
打印出 bash 内部正在做什么,而-c exit
只是告诉 bash 立即终止。 Using sed
to filter out the source
command or the .
使用sed
过滤掉source
命令或.
alias.别名。
/bin/bash -lixc exit 2>&1 | sed -n 's/^+* \(source\|\.\) //p'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.