What is the best way to set up a Bash script that prints each command before it executes it?
That would be great for debugging purposes.
I already tried this:
CMD="./my-command --params >stdout.txt 2>stderr.txt"
echo $CMD
`$CMD`
It's supposed to print this first:
./my-command --params >stdout.txt 2>stderr.txt
And then execute ./my-command --params
, with the output redirected to the files specified.
`$CMD`
to"$CMD"
. When bash encounters`$CMD`
, it replaces$CMD
with the command and you're left with`./my-command --params >stdout.txt 2>stderr.txt`
. Since this is wrapped in backticks, bash will execute the command, and instead of printing the output to stdout, it will replace the expression including the backticks with the output of the command, and since this ends up in the beginning of a command line, bash will try to interpret the output as a new command, which is not what you want. – HelloGoodbyePS4
variable in order to define the prompt displayed as prefix of commands in tracing output: thegeekstuff.com/2008/09/… – Ioannis Filippidiseval "$CMD"
, but that itself has serious security risks, which is why FAQ #50 doesn't suggesteval
but instead teaches use of arrays and functions. – Charles Duffy