1

How do I query the compile-time options of bash on a given system?

The system rc path for bash differs across systems. Sometimes it is /etc/bash.bashrc and sometimes it is /etc/bashrc. How can I detect this programmatically?

I know I can list options in a shell with:

set -o

or

shopt
  • (and sometimes it isn't anything at all ... at least on macOS, both the built-in and homebrew bash don't source /etc/bashrc unless told to) – muru Jul 7 at 17:30
2

As far as I can tell, Bash source code doesn't differentiate between SYS_BASHRC and other included rc files after compilation. In addition, SYS_BASHRC could be undefined, and the resulting binary wouldn't use a system rc at all.

All the files used by a process can be found out by strace, however. Bash includes rc files only if it is run interactively, so:

echo | strace -e openat -o tmp.log bash -i 2>/dev/null

The resulting file tmp.log will contain the information wanted:

openat(AT_FDCWD, "/etc/bash.bashrc", O_RDONLY) = 3

Unfortunately, it will also contain large numbers of lines e.g. for libraries (and the redirection to /dev/null). I'm not sure how to select the correct line in every case. But in practice I think it will most probably be the first non-library in /etc/:

grep -v O_CLOEXEC tmp.log | grep \"/etc | head -n 1 | sed -e 's/.*"\(.*\)".*/\1/'
  • 1
    You can tell bash to log the files: PS4='$(echo "${BASH_SOURCE}" >> tmp.log)' bash -ixc true 2>/dev/null, followed by awk '!a[$0]++' tmp.log – muru Jul 7 at 18:24
  • You're right, that's a neater solution. Though I'd use sort -u (or just head -n 1) instead of awk :) – Jaripekka Juhala Jul 7 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.