gpt4 book ai didi

zsh - 为什么要使用 zsh 数组参数?

转载 作者:行者123 更新时间:2023-12-04 02:58:39 27 4
gpt4 key购买 nike

list=(1 2 3)
for i in $list; do echo $i; done;
for i in $list[@]; do echo $i; done;
for i in $list[*]; do echo $i; done;
for i in ${list}; do echo $i; done;
for i in ${list[@]}; do echo $i; done;
for i in ${list[*]}; do echo $i; done;
for i in "${list[@]}"; do echo $i; done;

所有这些打印相同的东西:
1 
2
3

这些
for i in "$list"; do echo $i; done;
for i in "${list}"; do echo $i; done;
for i in "$list[*]"; do echo $i; done;
for i in "${list[*]}"; do echo $i; done;

所有打印品
1 2 3

我什么时候应该使用括号与不使用括号和 @对比 * ?例如,我 know the difference between "${list[@]}""${list[*]}" ,但我一直无法找到有关 $list 之间差异的直接答案。 , ${list} , $list[@] , ${list[@]} , $list[*] , 和 ${list[*]} .当我可以做 $list 时,为什么我还要使用数组参数? ?

同样,我为什么不只是 "$list"当我想要一个字符串中的数组中的所有元素时?在那种情况下,我疯狂地 4 不同的选项: "$list" , "${list}" , "$list[*]" , "${list[*]}" .

最佳答案

一般来说,使用哪种取决于我们。但是深入研究为什么 zsh 允许编写这样的代码可能是值得的。

来自 zsh FAQ 文档:

1.2: What is it?

Zsh is a UNIX command interpreter (shell) which of the standard shells most resembles the Korn shell (ksh);

--- Chapter 1: Introducing zsh and how to install it, Z-Shell Frequently-Asked Questions



和,

Chapter 2: How does zsh differ from...?

As has already been mentioned, zsh is most similar to ksh, while many of the additions are to please csh users.

...

2.1: Differences from sh and ksh

Most features of ksh (and hence also of sh) are implemented in zsh; problems can arise because the implementation is slightly different.

--- Chapter 2: How does zsh differ from...?, Z-Shell Frequently-Asked Questions



我告诉自己应该注意,当我编写自己的 zsh 脚本时,zsh 有很多类似 ksh 的功能和仿真选项。您可以尝试使用 setopt ksh_arrays 看看发生了什么,也是。

Arrays are (by default) more csh-like than ksh-like: subscripts start at 1, not 0; array[0] refers to array[1]; $array refers to the whole array, not $array[0]; braces are unnecessary: $a[1] == ${a[1]}, etc. Set the KSH_ARRAYS option for compatibility.

--- 2.1: Differences from sh and ksh, Z-Shell Frequently-Asked Questions



它可以通过显示与 ksh 的一些比较来提供一些提示/答案。

括号与无括号

Q22. Why are the braces required with array references, e. g. ${x[1]}?

A22. It would be nice to do $x[1], but the POSIX shell would expand $x and then search for the file pattern resulting by concatenating [1]. ksh is POSIX compatible.

--- III SHELL PROGRAMMING QUESTIONS Q22, KSH-93 - Frequently Asked Questions



ksh 处理数组参数是这样的:
list=(a b c)
echo $list[1]
;# => "a[1]"
;# concatination of the first $list element and "[1]" string
;# rather than below!
echo ${list[1]} ;# => "b"

因此,它可以用于 $list 中的任何一个, ${list} , $list[@] , ${list[@]} , $list[*] , 和 ${list[*]}在第一个例子中;它可以被认为是 zsh 的特性。

您可以通过阅读上面 ksh 文档的 $x[1] 从另一个角度查看代码。至 $list[@]$list[*] .

注意:在 zsh 中,如果 $list包含空值, "${list[@]}"因“ 24. Empty argument removal”而异。

24. Empty argument removal

If the substitution does not appear in double quotes, any resulting zero-length argument, whether from a scalar or an element of an array, is elided from the list of arguments inserted into the command line.

--- 24. Empty argument removal, Rules, Expansion, zshparam(1)


@对比 *

Q1. What is the difference between * and @, for example, and ?

A1. When used outside of "", they are equivalent. However, within double quotes, "$@" produces one argument for each positional parameter, and "$*" produces a single argument. Note that "$@" preserves arguments lists, whereas $* may not unless both word splitting and pathname expansion are disabled.

--- III SHELL PROGRAMMING QUESTIONS Q1, KSH-93 - Frequently Asked Questions



如您所知,前半部分与 zsh 相同。这是您建议的 zsh 文档的相同引用:

A subscript of the form [*] or [@] evaluates to all elements of an array; there is no difference between the two except when they appear within double quotes.

"$foo[*]" evaluates to "$foo[1] $foo[2] ...", whereas "$foo[@]" evaluates to "$foo[1]" "$foo[2]" ....

...

When an array parameter is referenced as $name (with no subscript) it evaluates to $name[*],

--- Array Subscripts, zshparam(1)


"$list"与其他人

如您所见,已经很清楚了,zsh 为我们提供了 4 个不同的选项作为其功能。但我认为 ksh 用户可以说:
"$list" , "${list}""$list[*]"可能意味着它只会对 $list 的第一个元素进行一些操作。 (以及为后者连接 "[*]" 的结果)而不是列表/数组引用。

这是一个示例代码:
list=(1 2 '' 3) # XXX: added an empty entry to check the difference

test-list-dq () {
echo "$1"
local i=
echo '$list:'; for i in $list; do echo $i; done;
echo '$list[@]:'; for i in $list[@]; do echo $i; done;
echo '$list[*]:'; for i in $list[*]; do echo $i; done;
echo '${list}:'; for i in ${list}; do echo $i; done;
echo '${list[@]}:'; for i in ${list[@]}; do echo $i; done;
echo '${list[*]}:'; for i in ${list[*]}; do echo $i; done;
echo '"${list[@]}":'; for i in "${list[@]}"; do echo $i; done;
}

test-list-nq () {
echo "$1"
local i=
for i in "$list"; do echo $i; done
for i in "${list}"; do echo $i; done
for i in "$list[*]"; do echo $i; done
for i in "${list[*]}"; do echo $i; done
}

echo "*double quotes"
test-list-dq "*default"
() {
setopt localoptions ksharrays no_nomatch
test-list-dq "*ksharrays on"
}

echo "*no quotes"
test-list-nq "*default"
() {
setopt localoptions ksharrays no_nomatch
test-list-nq "*ksharrays on"
}

输出如下:
*double quotes
*default
$list:
1
2
3
$list[@]:
1
2
3
$list[*]:
1
2
3
${list}:
1
2
3
${list[@]}:
1
2
3
${list[*]}:
1
2
3
"${list[@]}":
1
2

3
*ksharrays on
$list:
1
$list[@]:
1[@]
$list[*]:
1[*]
${list}:
1
${list[@]}:
1
2
3
${list[*]}:
1
2
3
"${list[@]}":
1
2

3
*no quotes
*default
1 2 3
1 2 3
1 2 3
1 2 3
*ksharrays on
1
1
1[*]
1 2 3

关于zsh - 为什么要使用 zsh 数组参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51411577/

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