以下のスクリプトを実行する。
log.sh
#!/bin/bash
# via. http://qiita.com/kawaz/items/65cdbeaa739c4e6b7776
export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME:+$FUNCNAME(): }'
bash -lx -c 'echo 1' 2> xtrace.log
$ ./log.sh
後は、grep export xtrace.log
するなり何なり。
$ grep export xtrace.log
+(/etc/profile:4): eval 'PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Users/takumiabe/.rbenv/shims:/Users/takumiabe/.rbenv/bin:/Users/takumiabe/go/bin:~/dot_files
/bin:/usr/local/sbin:/usr/local/opt/coreutils/libexec/gnubin";' export 'PATH;'
++(/etc/profile:4): export PATH
+(/Users/takumiabe/.bash_profile:4): export 'PS1=[ \[\e[32m\]\u@\h:\w\[\e[0m\] ]\[\e[0;31m\]$(__git_ps1)\[\e[0m\] \n\$ '
+(/Users/takumiabe/.bash_profile:11): export HISTCONTROL=ignoredups:ignorespace
+(/Users/takumiabe/.bash_profile:13): export HISTSIZE=2000
...
参考:
PS4の設定でシェルスクリプトのデバッグが捗る http://qiita.com/kawaz/items/65cdbeaa739c4e6b7776