有没有办法在 ZSH 中将字符串拆分为 space(" ") 分隔值的数组?

Is there a way to split a string into an array with space(" ") separated values in ZSH?

我有这个代码:

carp="2 hello 192.180.00.00"
array=( ${carp} )
echo "\n${array[2]}"

我正在尝试将 carp 拆分为一个名为 array 的数组。我的输出应该是“你好”,但它只是打印空白。有没有简单的将鲤鱼拆分成数组的方法?

zsh 有一个 parameter expansion 语法:${=spec}。要修改您的示例,它将是:

carp="2 hello 192.180.00.00"
array=( ${=carp} )
echo "\n${array[2]}"

描述是:

Perform word splitting using the rules for SH_WORD_SPLIT during the evaluation of spec, but regardless of whether the parameter appears in double quotes; if the ‘=’ is doubled, turn it off. This forces parameter expansions to be split into separate words before substitution, using IFS as a delimiter. This is done by default in most other shells.