Search code examples
bashloggingverbose

Bash verbose command with evaluating parameters


I am trying to log the command with its parameters (after evaluating if necessary) in Bash.

I am trying to use set -v:

$ variable=2
$ set -v
$ sleep $variable
sleep $variable

As you can see, it prints sleep $variable. I want to log sleep 2 instead.

My original command is more complex, so I don't want to echo each parameter one by one. (And it is probably more error prone to do so).


Solution

  • set -v (or set -o verbose) will output every command as it is read, without expanding things.

    set -x (or set -o xtrace) will output the expanded command line before execution. Each line is prepended by the PS4 prompt (usually a +) and for commands executed as part of command substitutions, the prompt will be "doubled up" (++).

    The trace will be written to the file descriptor indicated by $BASH_XTRACEFD (or to the shell's standard error by default).