对所有子文件夹运行任意zsh命令

对所有子文件夹运行任意zsh命令,zsh,Zsh,我目前正在使用此函数为zsh中的所有子文件夹运行命令 forsubdirs() { for dir in *; do (cd ${dir} && echo $fg_bold[yellow]${PWD##*/}$reset_color && $@ && echo '\n') done } 我这样使用它:forsubdirs git pull 但问题是:它不适用于别名。如何对所有子文件夹执行任意ZSH命令(包括别名和以“&”或“

我目前正在使用此函数为zsh中的所有子文件夹运行命令

forsubdirs() {
   for dir in *; do
     (cd ${dir} && echo $fg_bold[yellow]${PWD##*/}$reset_color && $@ && echo '\n')
   done
}
我这样使用它:
forsubdirs git pull

但问题是:它不适用于别名。如何对所有子文件夹执行任意ZSH命令(包括别名和以“&”或“;”)分隔的命令列表?

为了能够将复杂命令作为参数传递,您需要引用语法元素,如
&
。然后需要使用
eval
命令显式计算参数。例如:

forsubdirs () {
    for dir in *(/) ; do
        ( cd $dir && echo $fg_bold[yellow]${PWD##*/}$reset_color && eval $@ && echo '\n' )
    done
}

forsubdir 'ls -1 | sed "s/^/    /"'

另外,我建议使用
*(/)
而不是普通的
*
。它只匹配目录,因此该函数甚至不会尝试在常规文件上运行
cd

谢谢,但我认为有一个小错误:“%fg\u bold”不应该是“$fg\u bold”?@lonelyass是的,没错。我现在已经修好了。谢谢