Related to this question on shell scripts and echoing command: In a shell script: echo shell commands as they are executed
I'd like to do something like this:
foo() {
cmd='ls -lt | head'
echo $cmd
eval ${cmd}
}
I tried this:
foo2() {
set -x
ls -lt | head
set +x
}
but that generates this extra ouput
+foo2:2> ls -G -lt
+foo2:2> head
total 136
drwxr-xr-x 18 justin staff 612 Nov 19 10:10 spec
+foo2:3> set +x
Is there any more elegant way to do this in a zsh function?
I'd like to do something like this:
foo() {
cmd='ls -lt | head'
eval -x ${cmd}
}
and just echo the cmd being run (maybe with expansion of aliases).