zsh神秘变量展开式

zsh神秘变量展开式,zsh,Zsh,我在prezto源代码中找到了这个: # Set the command name, or in the case of sudo or ssh, the next command. local cmd="${${2[(wr)^(*=*|sudo|ssh|-*)]}:t}" 我读了很多书,但都不知道这是怎么回事。在shell本身的实验中,似乎表明[]是一些算术的东西,这是有意义的,但我没有看到解释(w)应该如何工作的部分。这似乎是一些神奇的运算符,适用于数学表达式 slu@ubuntu-slu

我在
prezto
源代码中找到了这个:

# Set the command name, or in the case of sudo or ssh, the next command.
local cmd="${${2[(wr)^(*=*|sudo|ssh|-*)]}:t}"
我读了很多书,但都不知道这是怎么回事。在shell本身的实验中,似乎表明
[]
是一些算术的东西,这是有意义的,但我没有看到解释
(w)
应该如何工作的部分。这似乎是一些神奇的运算符,适用于数学表达式

slu@ubuntu-sluvm ~/.zprezto ❯❯❯ VAR="one two three four"
slu@ubuntu-sluvm ~/.zprezto ❯❯❯ echo ${VAR[2]}
n
slu@ubuntu-sluvm ~/.zprezto ❯❯❯ echo ${VAR[(w)2]}
two
slu@ubuntu-sluvm ~/.zprezto ❯❯❯ echo ${VAR[(w)]}
zsh: bad math expression: empty string
slu@ubuntu-sluvm ~/.zprezto ❯❯❯

乍一看,它看起来很凌乱,但一旦你把它分成几个部分,它就相当简单了。这是ZSH中参数扩展和扩展全局支持的一个示例。如果您查看此代码示例所在的位置,您将看到它们设置为:

emulate-L zsh
setopt扩展_GLOB
现在让我们把你的线分开:

${
${
2[#展开第二个参数
(wr)#匹配一个单词
^(*=*|=| sudo | ssh |-*)不匹配*=*、=、sudo、ssh或-*
]
}
:t}#如果是路径,则仅返回文件名
您可以通过如下方式创建示例脚本来测试这一点:

#/bin/zsh
仿真-lzsh
setopt扩展_GLOB
echo“${$1[(wr)^(*=*|sudo | ssh |-*)]}:t}”将2更改为1,否则相同
以下是它的输出:

$./test.sh'/bin/zsh'
zsh
$./test.sh“sudo测试”
测试
$./test.sh“sudo——标志测试”
测试
$./test.sh'ssh-o=值测试'
测试
$./test.sh“test”
测试

有关更多信息,请参阅和。

我想主要的分歧在于难以从谷歌搜索结果中调出第15节