1

sh, bash, etc. have a -x flag that will show you each command that is executing in a script. This is handy for debugging.

$ sh -x my_script.sh

+ case $SOMETHING_DIR in
+ unset SOMETHING_SCRIPT_SOURCE
+ something_process_parameters
+ local SOMETHING_AUTO_MODE
+ SOMETHING_AUTO_MODE=use
+ something_supports_source_options
++ something_echo '[ $# -gt 0 ] && something_echo $1'
++ command printf '%s\n' '[ $# -gt 0 ] && something_echo $1'
++ . /dev/stdin yes

Is there a way to do this with source ?

$ source my_source.sh
0
5

Yes, you could use set -x (or set -o xtrace) to enable tracing in the current shell before sourcing the dot script, and then use set +x (or set +o xtrace) to disable tracing afterward.

set -x
source ./my_source.sh
set +x

Note that you might not get the expected output if the script you source sets or unsets the xtrace shell option.

2
  • 1
    See also: emulate sh -xc 'source ./my_source.sh' or emulate sh -o xtrace -c 'source ./my_source.sh' in zsh to source the file in sh emulation (the .sh extension suggests it's in sh syntax rather than zsh syntax) and with the xtrace option enabled. Note that any change to option settings done within my_source.sh would be lost afterwards. Also note the ./ prefix to make sure the file is not looked up in $PATH. Sep 27 at 7:40
  • @StéphaneChazelas I added a gentle dusting of that to my answer. Thank you!
    – they
    Sep 27 at 9:38

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