${var}、“$var"和“${var}"之间有什么区别?在 Bash shell 中? [英] What is the difference between ${var}, "$var", and "${var}" in the Bash shell?
问题描述
标题所说的:将变量封装在 {}
、""
或 "{}
" 中是什么意思?我无法在网上找到任何关于此的解释 - 除了使用符号外,我无法参考它们,这不会产生任何结果.
这是一个例子:
声明 -a 组groups+=("CN=exampleexample,OU=exampleexample,OU=exampleexample,DC=example,DC=com")groups+=("CN=示例,OU=示例,OU=示例,DC=示例,DC=com")
这个:
for group in "${groups[@]}";做回声$组完毕
事实证明与此大不相同:
for group in $groups;做回声$组完毕
还有这个:
for group in ${groups};做回声$组完毕
只有第一个完成了我想要的:遍历数组中的每个元素.我不太清楚 $groups
、"$groups"
、${groups}
和 "${ 之间的区别组}"
.如果有人能解释一下,我将不胜感激.
作为一个额外的问题 - 有没有人知道引用这些封装的公认方式?
Braces ($var
vs. ${var}
)
在大多数情况下,$var
和 ${var}
是相同的:
var=foo回声 $var# 富回声 ${var}# 富
大括号仅用于解决表达式中的歧义:
var=foo回声 $varbar# 不打印任何内容,因为没有变量 'varbar'回声 ${var}bar# foobar
引用($var
vs. "$var"
vs. "${var}"
)
当您在变量周围添加双引号时,您会告诉 shell 将其视为单个单词,即使它包含空格:
var="foo bar"因为我在$var"中;do # 扩展为 'for i in "foo bar";做...'echo $i # 所以只运行一次循环完毕# foo 栏
将该行为与以下内容进行对比:
var="foo bar"因为我在 $var 中;do # 扩展为 'for i in foo bar;做...'echo $i # 所以循环运行两次,每个参数一次完毕# 富# 酒吧
与 $var
和 ${var}
一样,大括号仅用于消除歧义,例如:
var="foo bar"因为我在$varbar"中;do # 扩展为 'for i in "";做...'因为没有echo $i # 变量名为'varbar',所以循环运行一次done # 不打印任何内容(实际上是 "")var="foo 酒吧"对于我在${var}bar"中;do # 在 "foo barbar" 中扩展为 'for i;做...'echo $i # 所以运行一次循环完毕# foo 酒吧
注意上面第二个例子中的 "${var}bar"
也可以写成 "${var}"bar
,在这种情况下你不需要不再需要大括号,即 "$var"bar
.但是,如果您的字符串中有很多引号,这些替代形式可能会变得难以阅读(因此难以维护).此页面很好地介绍了 Bash 中的引用.>
数组($var
vs. $var[@]
vs. ${var[@]}
)
现在为您的阵列.根据bash手册:
<块引用>引用一个没有下标的数组变量相当于引用一个下标为0的数组.
换句话说,如果你没有用 []
提供索引,你会得到数组的第一个元素:
foo=(a b c)回声 $foo# 一种
完全一样
foo=(a b c)回声 ${foo}# 一种
要获取数组的所有元素,您需要使用 @
作为索引,例如${foo[@]}
.数组需要大括号,因为没有它们,shell 将首先扩展 $foo
部分,给出数组的第一个元素,后跟文字 [@]
:
foo=(a b c)回声 ${foo[@]}# a b c回声 $foo[@]# 一种[@]
本页很好地介绍了 Bash 中的数组.
引用重新访问(${foo[@]}
vs. "${foo[@]}"
)
你没有问这个问题,但这是一个很好的细微差别,了解一下.如果数组中的元素可能包含空格,则需要使用双引号,以便将每个元素视为单独的单词:"
foo=(第一个"第二个")对于我在${foo[@]}"中;do # 在第一个"第二个"中扩展为 'for i;做...'echo $i # 所以循环运行两次完毕# 首先# 第二
将此与没有双引号的行为进行对比:
foo=(第一个"第二个")对于我在 ${foo[@]};do # 在第一个第二个中扩展为 'for i;做...'echo $i # 所以循环运行四次!完毕# 这# 第一的# 这# 第二
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.
Here's an example:
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")
This:
for group in "${groups[@]}"; do
echo $group
done
Proves to be much different than this:
for group in $groups; do
echo $group
done
and this:
for group in ${groups}; do
echo $group
done
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?
Braces ($var
vs. ${var}
)
In most cases, $var
and ${var}
are the same:
var=foo
echo $var
# foo
echo ${var}
# foo
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
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
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
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.
Arrays ($var
vs. $var[@]
vs. ${var[@]}
)
Now for your array. According to the bash manual:
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
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.
Quotes revisited (${foo[@]}
vs. "${foo[@]}"
)
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
这篇关于${var}、“$var"和“${var}"之间有什么区别?在 Bash shell 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!