Linux – How to make “bash -x” (debug mode) recurse into sourced scripts

bashlinuxmacos

I'm trying to debug what bash does on login initialization for Linux. I've read that "bash -x" will make bash print out what it's doing, but it doesn't print commands in sourced files like "set -x" does. I can't use "set -x" because initialization runs before I can call it. "bash -x" seems to recurse fine on OS X, but that might be because of the bash versions.

Linux: 3.2.25

OS X: 3.2.48

Here's an excerpt of the non-recursing behavior on Linux:

bash -l -x -c 'echo 1'
# ... snip ...
+ for i in '/etc/profile.d/*.sh'
+ '[' -r /etc/profile.d/vim.sh ']'
+ '[' '' ']'
+ . /etc/profile.d/vim.sh
+ for i in '/etc/profile.d/*.sh'
+ '[' -r /etc/profile.d/which-2.sh ']'
+ '[' '' ']'
+ . /etc/profile.d/which-2.sh
# ... snip ...

Notice how /etc/profile.d/vim.sh is sourced, but its commands aren't printed.
Is there a workaround without upgrading? Is this caused by the version difference?

Best Answer

set -x somewhere in the root file (the one requiring all other files) should work. Alternatively, introduce something like [[ !-z "$DEBUG" ]] && set -x in each file, and call with DEBUG=1 script.sh.

Related Question