找出启动时读取的所有文件bash/dash/sh

Find out all the files bash / dash / sh reads during startup

上下文

成果

$ strace -fe trace=%file bash -c exit

execve("/usr/bin/bash", ["bash", "-c", "exit"], 0x7ffc430c3870 /* 67 vars */) = 0
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libtinfo.so.6", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libdl.so.2", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/dev/tty", O_RDWR|O_NONBLOCK) = 3
openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache", O_RDONLY) = 3
stat("/home/user", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat(".", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/home/user", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat(".", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
stat("/home/user/.cargo/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/.local/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/.local/opt/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/.cargo/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/.npm/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/.cargo/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/.local/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/.local/opt/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/.cargo/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/.npm/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/home/user/workspace/go/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/usr/local/sbin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/usr/local/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory)
stat("/usr/sbin/bash", 0x7ffe62a61e90)  = -1 ENOENT (No such file or directory)
stat("/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1166912, ...}) = 0
stat("/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1166912, ...}) = 0
access("/usr/bin/bash", X_OK)           = 0
stat("/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1166912, ...}) = 0
access("/usr/bin/bash", R_OK)           = 0
stat("/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1166912, ...}) = 0
stat("/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1166912, ...}) = 0
access("/usr/bin/bash", X_OK)           = 0
stat("/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1166912, ...}) = 0
access("/usr/bin/bash", R_OK)           = 0
+++ exited with 0 +++

运行 plain strace -f bash |& grep profile 之类的似乎也什么都没显示。

问题

  1. 如何列出 shell 最终实际读取的所有文件? (请注意,问题不是根据文档应该阅读的内容,而是 being 在 运行time 中实际阅读的内容)
  2. 为什么上面没有显示任何 .profile/etc/profile/etc/bashrc .bashrc 等?
  3. (有点偏离主题)为什么这里会出现一系列 ENOENT - stat("/home/user/.cargo/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory) - 它似乎在所有 env 路径上搜索 bash。但是为什么会这样呢? (如何在此处找到实际执行 bash 命令的内容,这会导致 PATH 搜索)

您正在查看 shell 正在读取的所有文件。

/etc/profile~/.profile~/.bash_profile~/.bash_login 是 bash 仅在作为 login shell 调用时才读取的登录文件。 /etc/bashrc(或/etc/bash.bashrc)和~/.bashrc 是交互式会话的自定义文件(这里有不相关的怪癖)。 None 这些文件在解释脚本或 运行 使用 -c 执行单个命令时被读取。

Bash 确实在启动时读取名称在 BASH_ENV 环境变量中的文件。但通常此环境变量未设置,因此 bash 不读取任何文件。

Bash 显然喜欢在启动时获取有关其自身二进制文件的信息。我不知道那是干什么用的,但它是内置行为,而不是来自某些启动脚本的行为。

其中的

None 将以任何方式帮助您理解为什么包含 bash 构造的脚本会 运行 带有破折号。脚本是 运行 和破折号的原因可能是 /bin/sh 在您的系统上是破折号:系统很少显式调用 dash。如果您不知道是哪个脚本导致的,或者如果您不了解该脚本是如何调用的,则您的调查应该从消息出现的情况开始。如果它是登录时脚本,您需要了解登录在您的系统上是如何工作的以及调用了哪些程序。 Unix & Linux 可能会有所帮助。