什么是$ {VAR}&QUOT之间的区别; $ {VAR}" $ VAR"和"在Bash shell中?
什么标题说:这是什么意思封装在变量{}
,,
或{}
?我一直没能在网上找到任何解释这个 - 我一直没能指他们只是使用的符号,它不产生任何
What the title says: what does it mean to encapsulate a variable in {}
, ""
, or "{}
"? I haven't been able to find any explanations online about this - I haven't been able to refer to them except for using the symbols, which doesn't yield anything.
下面是一个例子:
declare -a groups
groups+=("CN=exampleexample,OU=exampleexample,OU=exampleexample,DC=example,DC=com")
groups+=("CN=example example,OU=example example,OU=example example,DC=example,DC=com")
这
for group in "${groups[@]}"; do
echo $group
done
被证明是很大的不同莫过于:
Proves to be much different than this:
for group in $groups; do
echo $group
done
和这样的:
for group in ${groups}; do
echo $group
done
只有第一个完成我想要的东西:通过数组中的每个元素进行迭代。我不是 $组
,$组
,之间的区别真正明确$ {组}
和$ {}组
。如果任何人都可以解释的话,我会AP preciate它。
Only the first one accomplishes what I want: to iterate through each element in the array. I'm not really clear on the differences between $groups
, "$groups"
, ${groups}
and "${groups}"
. If anyone could explain it, I would appreciate it.
作为一个额外的问题 - 没有人知道接受的方式来指代这些封装
As an extra question - does anyone know the accepted way to refer to these encapsulations?
在大多数情况下, $ VAR
和 $ {VAR}
是相同的:
Braces ($var
vs. ${var}
)
In most cases, $var
and ${var}
are the same:
var=foo
echo $var
# foo
echo ${var}
# foo
大括号只需要解决的前pressions歧义:
The braces are only needed to resolve ambiguity in expressions:
var=foo
echo $varbar
# Prints nothing because there is no variable 'varbar'
echo ${var}bar
# foobar
行情( $ VAR
与$ VAR
与$ { VAR}
)
在你身边的一个变量加双引号,你告诉shell把它当作一个字,即使它包含空格:
Quotes ($var
vs. "$var"
vs. "${var}"
)
When you add double quotes around a variable, you tell the shell to treat it as a single word, even if it contains whitespaces:
var="foo bar"
for i in "$var"; do # Expands to 'for i in "foo bar"; do...'
echo $i # so only runs the loop once
done
# foo bar
对比度与以下行为:
Contrast that behavior with the following:
var="foo bar"
for i in $var; do # Expands to 'for i in foo bar; do...'
echo $i # so runs the loop twice, once for each argument
done
# foo
# bar
与 $ VAR
与 $ {VAR}
,只需要消除歧义括号,例如
As with $var
vs. ${var}
, the braces are only needed for disambiguation, for example:
var="foo bar"
for i in "$varbar"; do # Expands to 'for i in ""; do...' since there is no
echo $i # variable named 'varbar', so loop runs once and
done # prints nothing (actually "")
var="foo bar"
for i in "${var}bar"; do # Expands to 'for i in "foo barbar"; do...'
echo $i # so runs the loop once
done
# foo barbar
注意在第二个例子也可以写成$ {VAR}栏
$ {VAR}栏
,在这种情况下,你不需要再花括号,即$ VAR栏
。但是,如果你有很多字符串中的引号,这些替代形式可以得到难以阅读(因而难以维持)。 本页面提供了一个很好的介绍中猛砸报价。
Note that "${var}bar"
in the second example above could also be written "${var}"bar
, in which case you don't need the braces anymore, i.e. "$var"bar
. However, if you have a lot of quotes in your string these alternative forms can get hard to read (and therefore hard to maintain). This page provides a good introduction to quoting in Bash.
现在您的阵列。按照 bash的手动:
引用数组变量无标相当于引用为0的下标数组
Referencing an array variable without a subscript is equivalent to referencing the array with a subscript of 0.
在换句话说,如果你不提供与指数 []
,你得到的数组的第一个元素:
In other words, if you don't supply an index with []
, you get the first element of the array:
foo=(a b c)
echo $foo
# a
这是完全一样的。
Which is exactly the same as
foo=(a b c)
echo ${foo}
# a
要得到一个数组的所有元素,你需要使用 @
为指标,例如 $ {foo的[@]}
。大括号都需要使用数组,因为没有他们,外壳将首先展开 $ foo的
部分,提供数组后跟文字的第一个元素 [@]
:
To get all the elements of an array, you need to use @
as the index, e.g. ${foo[@]}
. The braces are required with arrays because without them, the shell would expand the $foo
part first, giving the first element of the array followed by a literal [@]
:
foo=(a b c)
echo ${foo[@]}
# a b c
echo $foo[@]
# a[@]
本页面是一个很好的介绍中击到数组。
This page is a good introduction to arrays in Bash.
您没问这个,但它是一个微妙的差异这是很好的了解。如果你的数组中的元素可以包含空格,则需要用双引号,这样每个元素都被作为一个单独的词:治疗
You didn't ask about this but it's a subtle difference that's good to know about. If the elements in your array could contain whitespace, you need to use double quotes so that each element is treated as a separate "word:"
foo=("the first" "the second")
for i in "${foo[@]}"; do # Expands to 'for i in "the first" "the second"; do...'
echo $i # so the loop runs twice
done
# the first
# the second
与行为对比这没有双引号:
Contrast this with the behavior without double quotes:
foo=("the first" "the second")
for i in ${foo[@]}; do # Expands to 'for i in the first the second; do...'
echo $i # so the loop runs four times!
done
# the
# first
# the
# second