简体   繁体   English

bash $ {variable [*]}:2中此语法的含义是什么?

[英]What is the meaning of this syntax in bash ${variable[*]}:2?

I read a code of someone in bash, and he writed this line: 我读了一个bash某人的代码,他写下了这一行:

for f in ${variable[*]}:2

Can someone explain what is the meaning of this line? 有人可以解释这条线的意思吗? what is the scope of the values that f can get? f可以得到的值的范围是什么?

The code given is a buggy attempt to expand an array skipping the first two arguments. 给出的代码是错误的尝试,试图跳过前两个参数来扩展数组。

A corrected version would iterate over "${variable[@]:2}" , with the quotes (and the :2 inside the braces rather than outside them), as demonstrated below: 更正后的版本将遍历"${variable[@]:2}" ,并加上引号(以及:2在大括号内而不是在大括号外),如下所示:

variable=( "first argument" "second argument" "third argument" "fourth argument" "fifth argument" )
printf '%s\n' "${variable[@]:2}"

...properly emits: ...正确发出:

third argument
fourth argument
fifth argument

As it's already written, the code behaves as follows (and the below is not even a worst-case scenario, wherein the string would contain globs): 正如已经编写的那样,代码的行为如下(并且以下甚至不是最坏的情况,其中字符串将包含glob):

variable=( "first argument" "second argument" "third argument" "fourth argument" "fifth argument" )
printf '%s\n' ${variable[*]}:2  ## THIS IS BUGGY

...emits output of: ...发出以下内容的输出:

first
argument
second
argument
third
argument
fourth
argument
fifth
argument:2

Due to the missing quotes (and the use of [*] rather than [@] ), the individual words aren't kept together; 由于缺少引号(并且使用[*]而不是[@] ),因此各个单词不能保持在一起; and because the :2 is outside the curly braces, it's appended to the end of the last word of the result rather than changing how the expansion behaves. 由于:2在花括号外面,因此将其附加到结果的最后一个单词的末尾,而不用更改扩展的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM