First, I'd recommend simply using set -x
, which logs commands to standard error before they are executed. You can disable this after you are done with set +x
.
set -x
lsuser -f root|grep -i maxage|awk -F= '{print$2}'
set +x
Drawbacks:
- The data is written to standard error, not standard output. (In newer versions of
bash
, though, you can specify an arbitrary file descriptor by setting BASH_XTRACEFD
.)
- The execution of
set +x
is traced as well.
If you set the value of the variable very carefully, you can use eval
.
cmd="lsuser -f root|grep -i maxage|awk -F= '{print\$2}'"
echo "$cmd"
eval "$cmd"
However, I do not recommend this approach. It is, in theory, possible to correctly set the value of cmd
so that it both logs accurately and executes correctly and as intended, but I find it to be more trouble than it is worth. (However, I wait for someone to show how to do it easily; I know it involves printf
and the %q
specifier.)