2

Given a shell script (with conditions, concatenations and interpolations) that builds and executes certain commands (in my case calls to ImageMagick's convert cli), is there a way to list the commands that were executed in that script? Since the script calls convert multiple times substituting args, I'd like to get a list like the following.

convert ...
convert ...
convert ...
...

I'm using zsh in OS X, but can switch to bash, etc.

2 Answers 2

10

Stick a

set -x

at the top of your script. Works in bash, should work in zsh.

Sign up to request clarification or add additional context in comments.

4 Comments

If my script executes other scripts, is there a way to make this set -x affect all the nested contexts too? Maybe a global shell variable?
@hakunin: I do not think it is possible, but you can do strace -f -o >(grep '^[0-9]\+ \+exec' > script.strace) /path/to/script.zsh (I know about strace -e option, but I do not want to remember all variants of exec function). Note that it will catch only launching of external processes, it won't catch every shell command.
@hakunin: If all the scripts involved are zsh scripts, there's a way: add set -x to /tmp/somedir/.zshenv and run the script with ZDOTDIR=/tmp/somedir /path/to/script. For ksh scripts, set ENV=/tmp/env.sh where env.sh contains set -x. For bash scripts, set BASH_ENV.
If you ran "set -x" in your terminal directly, you can undo it by running "set +x"
4

In zsh (maybe only with oh-my-zsh enabled) I find there's a lot of noise produced with set -x or setopt xtrace.

But you can also:

typeset -ft <function_name>

to trace only one function.

To turn it off:

typeset +ft <function_name>

Adapted from the A User's Guide to the Z-Shell.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.