Bash is able to trace execution script commands with the -x
command line option. Each command is then output to stderr
, prefixed by PS4
as stated in the man page.
-x After expanding each simple command, for command, case command, select command, or arithmetic for command, display the expanded value of PS4, followed by the command and its expanded arguments or associated word list.
Albeit useful, the resolved executable path of each command is not given. So I have to figure out which executable has been run by guessing shell aliases and PATH
value while reading execution trace.
For example, ruby
has multiple command candidates on my workstation:
$ type -a ruby
ruby is /home/cbliard/.rvm/rubies/ruby-2.1.0/bin/ruby
ruby is /home/cbliard/.rvm/bin/ruby
ruby is /usr/bin/ruby
ruby is /home/cbliard/.rvm/bin/ruby
If executing this script with /bin/bash -x
#!/bin/bash
echo "compute"
ruby script/run_something.rb
echo "deploy"
bundle install
bundle exec cap deploy
Trace execution will be
+ echo "compute"
compute
+ ruby script/run_something.rb
[output of ruby script]
+ bundle install
Using rake (10.1.0)
Using i18n (0.6.9)
[...]
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
+ bundle exec cap deploy
triggering load callbacks
* 2014-01-14 09:12:42 executing `deploy'
* 2014-01-14 09:12:42 executing `deploy:update'
[...]
Finished: SUCCESS
I had some oddities with resolved executables, mainly due to my usage of rvm
. The output I would like is with fully resolved path, like this:
+ [builtin] echo "compute"
compute
+ /home/cbliard/.rvm/rubies/ruby-1.9.3-p484/bin/ruby script/run_something.rb
[output of ruby script]
+ /home/cbliard/.rvm/gems/ruby-1.9.3-p484@global/bin/bundle install
Using rake (10.1.0)
Using i18n (0.6.9)
[...]
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
+ /home/cbliard/.rvm/gems/ruby-1.9.3-p484@global/bin/bundle exec cap deploy
triggering load callbacks
* 2014-01-14 09:12:42 executing `deploy'
* 2014-01-14 09:12:42 executing `deploy:update'
[...]
Finished: SUCCESS
Having the resolved command would really help me debugging weird rvm
/bundler
issues. The -x
option helps a lot but sometimes it is not enough. Is there any trick that helps knowing the full characteristics of the commands while running a bash
script?. It is better if the original script does not have to be modified.