与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
附注:我意识到这个帖子太老了,无法回答最初的提问者,但我想在未来帮助任何遇到这个问题的人。
发布于 2013-11-20 05:40:47
这对我很有效。我定义了这个zsh函数:
echoRun() {
echo "> $1"
eval $1
}然后,我在一个函数中运行命令,如下所示:
foo() {
echoRun "ls -lt | head"
}还有更好的选择吗?
https://stackoverflow.com/questions/20082388
复制相似问题