在Bash中,标量(scalar)和单元素数组(single-element array)之间有什么区别?

huangapple go评论54阅读模式
英文:

In Bash, what is the difference between a scalar and single-element array?

问题

除了通过 declare -p 报告的差异外,标量是否与单元素数组不可区分? 将它们视为等同是否安全?

X="cat"   # 标量
Y=("cat") # 单元素数组

一般来说,如果在表达式中将数组视为标量,则会得到第一个数组元素。此外,如果将标量用作数组,则它会表现得像一个单元素数组。

declare -p 显示它们是不同的。(我在注释中显示命令输出。)

declare -p X Y # declare -- X="cat" 
               # declare -a Y=([0]="cat")

如果将数组视为标量,它会表现得像一个标量。

echo 字符串长度: ${#X} ${#Y}                   # 字符串长度: 3 3
echo 值: $X $Y                                # 值: cat cat
[ "$X" == "$Y" ] && echo "相等测试为真"  # 相等测试为真

如果将标量视为数组,它会表现得像一个单元素数组。

echo "数组长度:   ${#X[@]} ${#Y[@]}"  # 数组长度:   1 1
echo "数组索引: ${X[0]}  ${Y[0]}"   # 数组索引: cat cat 
echo "${X[1]}" "${Y[1]}"              #                 ""

"M. Nejat Aydin" 和 "Ed Morton" 的回复显示它们并不总是等同。谢谢。

英文:

Except for the difference in how the are reported by declare -p, is a scalar indistinguishable from a single-element array? Is it safe to treat them as equivalent?

X="cat"   # scalar
Y=("cat") # single-element array

In general, if you if you use an array in an expression as if it were a scalar, you get the first array element. Furthermore, if you use a scalar as an array, it acts like a single-element array.

declare -p shows that they are distinct. (I show command output in comments.)

declare -p X Y # declare -- X="cat" 
               # declare -a Y=([0]="cat")

If you treat the array as a scalar, it acts like a scalar.

echo string length: ${#X} ${#Y}                   # string length: 3 3
echo values: $X $Y                                # values: cat cat
[ "$X" == "$Y" ] && echo "equality test is true"  # equality test is true

If you treat the scalar like an array, it acts a single-element array.

echo "array length:   ${#X[@]} ${#Y[@]}"  # array length:   1 1
echo "array indexing: ${X[0]}  ${Y[0]}"   # array indexing: cat cat 
echo \"${X[1]}\" \"${Y[1]}\"              #                 ""  ""     

Responses from "M. Nejat Aydin" and "Ed Morton" show that they are not always equivalent. Thanks.

答案1

得分: 3

根据 man bash

如果使用语法 name[subscript]=value 赋值给任何变量,将自动创建一个 索引数组
...

引用一个数组变量(无论是 索引数组 还是 关联数组),如果没有下标,等同于使用下标 0 引用该数组。

英文:

According to man bash:

> An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value.
> ...
>
> Referencing an array <sup>(either indexed array or associative array)</sup> variable without a subscript is equivalent to referencing the array with a subscript of 0.

答案2

得分: 2

根据bash源代码

/* 执行数组赋值 name[ind]=value。如果NAME已经存在且不是数组,IND为0,则执行name=value。如果NAME存在且不是数组,IND不为0,则将其转换为数组,以现有值作为name[0]。

如果NAME不存在,无论IND的值如何,都只需创建一个数组变量。 */
SHELL_VAR *
bind_array_variable (name, ind, value, flags)
英文:

According to the bash sourcecode:

/* Perform an array assignment name[ind]=value.  If NAME already exists and
   is not an array, and IND is 0, perform name=value instead.  If NAME exists
   and is not an array, and IND is not 0, convert it into an array with the
   existing value as name[0].

   If NAME does not exist, just create an array variable, no matter what
   IND&#39;s value may be. */
SHELL_VAR *
bind_array_variable (name, ind, value, flags)

答案3

得分: 2

$ X="cat" # scalar
$ Y=("cat") # single-element array

$ echo "${X[@]:1}"
at
$ echo "${Y[@]:1}"
$

英文:
$ X=&quot;cat&quot;   # scalar
$ Y=(&quot;cat&quot;) # single-element array

<p>

$ echo &quot;${X[@]:1}&quot;
at
$ echo &quot;${Y[@]:1}&quot;
$

huangapple
  • 本文由 发表于 2023年6月15日 14:23:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定