gpt4 book ai didi

shell - 在zsh函数中,如何回显命令

转载 作者:行者123 更新时间:2023-12-01 19:07:34 25 4
gpt4 key购买 nike

与 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(可能会扩展别名)。

最佳答案

setopt 详细

将其放在您想要在命令运行时开始回显命令的任何位置,当您不希望出现这种行为时,请使用

unsetopt verbose

附注我意识到这个帖子太旧了,无法回答最初的提问者,但我想帮助将来遇到这个问题的任何人。

关于shell - 在zsh函数中,如何回显命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20082388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com