I discovered something weird in bash I can't explain. When I initialize an array using the bracket notation with a quoted string (single and double), the string is placed as the first element of the array. When I put the string in a variable and I do the same withe the variable, the string is split properly delimited by IFS.
我在bash中发现了一些奇怪的东西我无法解释。当我使用带引号的字符串(单引号和双引号)的括号表示法初始化数组时,字符串将作为数组的第一个元素放置。当我将字符串放在变量中并且我对变量执行相同操作时,字符串将被IFS正确分隔。
#/bin/bash
test1="hello my name is mr nobody"
array1=($test1)
test2='hello my name is mr nobody'
array2=($test2)
array3=("Hello my name is mr nobody")
array4=('Hello my name is mr nobody')
declare -p array1
declare -p array2
declare -p array3
declare -p array4
The output:
declare -a array1='([0]="hello" [1]="my" [2]="name" [3]="is" [4]="mr" [5]="nobody")'
declare -a array2='([0]="hello" [1]="my" [2]="name" [3]="is" [4]="mr" [5]="nobody")'
declare -a array3='([0]="Hello my name is mr nobody")'
declare -a array4='([0]="Hello my name is mr nobody")'
What exactly happening, and what is different between the two methods?
究竟发生了什么,两种方法有什么不同?
1 个解决方案
#1
There is no difference between a string and a string in a variable. Consequently, the following two are identical:
字符串和变量中的字符串没有区别。因此,以下两个是相同的:
> test1="hello my name is mr nobody"
> array1=($test1)
> array2=(hello my name is mr nobody)
So are the following two:
以下两个是:
> test2="hello my name is mr nobody"
> array3=("$test2")
> array4=("hello my name is mr nobody")
The string does not "remember" that some or all of its characters were quoted. The quotes are entirely syntactic, and are interpreted (once) by the bash interpreter.
字符串不“记住”引用了部分或全部字符。引号完全是语法的,并由bash解释器解释(一次)。
This is not significantly different from other languages: in C or Python, the string "abc"
has three characters, not five; the quotes were only used to indicate that the literal is a string. In bash, however, it is possible (sometimes) to write a string without quotes, something not allowed by many other languages.
这与其他语言没有显着差异:在C或Python中,字符串“abc”有三个字符,而不是五个字符;引号仅用于表示文字是一个字符串。但是,在bash中,(有时)可以编写一个没有引号的字符串,这是许多其他语言所不允许的。
Word splitting is performed on unquoted strings, so it is performed on $test1
and a string
, and not performed on the quoted versions "$test1"
and "a string"
.
对未加引号的字符串执行单词拆分,因此它在$ test1和字符串上执行,而不是在引用的版本“$ test1”和“a string”上执行。
#1
There is no difference between a string and a string in a variable. Consequently, the following two are identical:
字符串和变量中的字符串没有区别。因此,以下两个是相同的:
> test1="hello my name is mr nobody"
> array1=($test1)
> array2=(hello my name is mr nobody)
So are the following two:
以下两个是:
> test2="hello my name is mr nobody"
> array3=("$test2")
> array4=("hello my name is mr nobody")
The string does not "remember" that some or all of its characters were quoted. The quotes are entirely syntactic, and are interpreted (once) by the bash interpreter.
字符串不“记住”引用了部分或全部字符。引号完全是语法的,并由bash解释器解释(一次)。
This is not significantly different from other languages: in C or Python, the string "abc"
has three characters, not five; the quotes were only used to indicate that the literal is a string. In bash, however, it is possible (sometimes) to write a string without quotes, something not allowed by many other languages.
这与其他语言没有显着差异:在C或Python中,字符串“abc”有三个字符,而不是五个字符;引号仅用于表示文字是一个字符串。但是,在bash中,(有时)可以编写一个没有引号的字符串,这是许多其他语言所不允许的。
Word splitting is performed on unquoted strings, so it is performed on $test1
and a string
, and not performed on the quoted versions "$test1"
and "a string"
.
对未加引号的字符串执行单词拆分,因此它在$ test1和字符串上执行,而不是在引用的版本“$ test1”和“a string”上执行。