为什么 read -q 不能在 zsh 的双括号内使用?

Why can read -q not be used inside double brackets in zsh?

我的 .zshrc 中有以下内容:

#    if read -q; then
#        echo; zplug install
#    fi
     [[ read -q; ]] && echo; zplug install

被注释掉的部分有效,但我更喜欢在一行中写一些东西,所以我尝试使用一种不那么冗长的形式。为什么不起作用?

我得到/Users/brandon/.zshrc:62: parse error: condition expected: read

[[专门用于计算表达式,不是return代码。 if 变体的一行等价物是(a):

 read -q && { echo; zplug install }

如果您的 if 变体类似于:

if [[ something-or-other ]] ; then
    blah
fi

然后你会在你的一行中使用[[

[[ something-or-other ]] && blah

(a) 注意这里使用大括号对命令进行分组。命令 read -q && echo; zplug install 将无条件地 运行 zplug - 只有 echo 将受制于 read return 值。