Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Context

  • Certain modifications to my start up scripts ended up with dash: 1: [[: not found and similar with ash shells.
  • I naturally assumed it was some profile (.profile, etc) based script that used bash's [[ while it's being run with POSIX based shells. But couldn't figure out which one.
  • So decided to run it under strace to find out all the files being read. The result surprised me - none of the profile based files were listed in the strace output.

Outcomes

$ 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 +++

Running with plain strace -f bash |& grep profile and such also seem to show nothing.

Questions

  1. How do I list all the files that the shell ends up actually reading? (Note that the question is not what it's supposed to read according to the docs, but rather what is actually being read in runtime)
  2. Why does the above not show any of the .profile, /etc/profile, /etc/bashrc .bashrc etc?
  3. (A bit off topic) Why does a series of ENOENTs happen here - stat("/home/user/.cargo/bin/bash", 0x7ffe62a61e90) = -1 ENOENT (No such file or directory) - It appears it's searching for bash on all the env paths. But why does this happen? (How to find what is actually executing the bash command here which results in the PATH search)
like image 768
x70766c Avatar asked Sep 05 '25 03:09

x70766c


1 Answers

You are seeing all the files that the shell is reading.

/etc/profile, ~/.profile, ~/.bash_profile and ~/.bash_login are login files that bash only reads if it's invoked as a login shell. /etc/bashrc (or /etc/bash.bashrc) and ~/.bashrc are customization files for interactive sessions (with quirks that aren't relevant here). None of these files are read when interpreting a script or running a single command with -c.

Bash does read the file whose name is in the BASH_ENV environment variable when it starts. But normally this environment variable is unset so bash doesn't read any file.

Bash apparently likes to obtain information about its own binary when it starts. I don't know what that's for, but it's built-in behavior, not behavior that comes from some startup script.

None of this will help you in any way to understand why a script that contains bash constructs is being run with dash. The reason the script is run with dash is probably that /bin/sh is dash on your system: systems rarely invoke dash explicitly. If you don't know which script is causing this or if you don't understand how that script is called, your investigation should to start from the circumstances under which the message appears. If it's a login-time script, you need to understand how login works on your system and what programs are invoked. Unix & Linux may help with that.

like image 154
Gilles 'SO- stop being evil' Avatar answered Sep 07 '25 19:09

Gilles 'SO- stop being evil'