与shell脚本和回显命令相关的问题:In a shell script: echo shell commands as they are executed
我想做这样的事情:
foo() {
cmd='ls -lt | head'
echo $cmd
eval ${cmd}
}我试过这个:
foo2() {
set -x
ls -lt | head
set +x
}但这会产生额外的输出
+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在zsh函数中有没有更好的方法来实现这一点呢?
我想做这样的事情:
foo() {
cmd='ls -lt | head'
eval -x ${cmd}
}并且只需回显正在运行的cmd (可能带有别名的扩展)。
发布于 2016-03-01 01:52:34
setopt verbose
将其放在您想要在命令运行时开始回显命令的位置,当您不想要该行为时,使用
unsetopt verbose
附注:我意识到这个帖子太老了,无法回答最初的提问者,但我想在未来帮助任何遇到这个问题的人。
https://stackoverflow.com/questions/20082388
复制相似问题