New*_*ser 602 syntax bash shell curly-braces
在shell脚本中,我们{}
何时在扩展变量时使用?
例如,我见过以下内容:
var=10 # Declare variable
echo "${var}" # One use of the variable
echo "$var" # Another use of the variable
Run Code Online (Sandbox Code Playgroud)
是否存在显着差异,还是只是风格?一个比另一个更受欢迎吗?
Fre*_*Foo 691
在这个特定的例子中,它没有任何区别.但是,如果要在字符串中展开变量,则{}
in ${}
非常有用foo
"${foo}bar"
Run Code Online (Sandbox Code Playgroud)
因为"$foobar"
会改为扩大foobar
.
在以下情况下无条件地需要使用花括号:
${array[42]}
${filename%.*}
(删除扩展名)"$8 $9 ${10} ${11}"
在任何地方这样做,而不仅仅是在可能模糊的情况下,可以被认为是良好的编程实践.这既是为了保持一致性,也是为了避免出现意外情况,例如$foo_$bar.jpg
,下划线成为变量名称的一部分在视觉上并不明显.
Aar*_*aid 119
变量的声明,并没有分配$
,没有{}
.你必须使用
var=10
Run Code Online (Sandbox Code Playgroud)
分派.为了从变量中读取(换句话说,'展开'变量),必须使用$
.
$var # use the variable
${var} # same as above
${var}bar # expand var, and append "bar" too
$varbar # same as ${varbar}, i.e expand a variable called varbar, if it exists.
Run Code Online (Sandbox Code Playgroud)
这有时让我困惑 - 在其他语言中,我们以相同的方式引用变量,无论它是在赋值的左侧还是右侧.但是shell脚本是不同的,$var=10
不会做你认为它做的事情!
gle*_*man 30
您{}
用于分组.大括号需要取消引用数组元素.例:
dir=(*) # store the contents of the directory into an array
echo "${dir[0]}" # get the first entry.
echo "$dir[0]" # incorrect
Run Code Online (Sandbox Code Playgroud)
小智 26
您还可以在大括号内进行一些文本操作:
STRING="./folder/subfolder/file.txt"
echo ${STRING} ${STRING%/*/*}
Run Code Online (Sandbox Code Playgroud)
结果:
./folder/subfolder/file.txt ./folder
Run Code Online (Sandbox Code Playgroud)
要么
STRING="This is a string"
echo ${STRING// /_}
Run Code Online (Sandbox Code Playgroud)
结果:
This_is_a_string
Run Code Online (Sandbox Code Playgroud)
你是正确的"常规变量"是不需要的...但它对调试和阅读脚本更有帮助.
cod*_*ter 12
访问数组元素和执行大括号扩展总是需要大括号。
{}
即使没有歧义,也不要过度谨慎并用于 shell 变量扩展。
例如:
dir=log
prog=foo
path=/var/${dir}/${prog} # excessive use of {}, not needed since / can't be a part of a shell variable name
logfile=${path}/${prog}.log # same as above, . can't be a part of a shell variable name
path_copy=${path} # {} is totally unnecessary
archive=${logfile}_arch # {} is needed since _ can be a part of shell variable name
Run Code Online (Sandbox Code Playgroud)
因此,最好将三行写为:
path=/var/$dir/$prog
logfile=$path/$prog.log
path_copy=$path
Run Code Online (Sandbox Code Playgroud)
这绝对更具可读性。
由于变量名称不能以数字开头,壳不需要{}
围绕编号的变量(如$1
,$2
等),除非这样的膨胀被后跟数字。这太微妙了,它确实可以{}
在这种情况下明确使用:
set app # set $1 to app
fruit=$1le # sets fruit to apple, but confusing
fruit=${1}le # sets fruit to apple, makes the intention clear
Run Code Online (Sandbox Code Playgroud)
看:
Sri*_*bat 11
变量名称的结尾通常用空格或换行符表示.但是如果我们在打印变量值后不想要空格或换行符怎么办?花括号告诉shell解释器变量名称的结尾.
TIME=10
# WRONG: no such variable called 'TIMEsecs'
echo "Time taken = $TIMEsecs"
# What we want is $TIME followed by "secs" with no whitespace between the two.
echo "Time taken = ${TIME}secs"
Run Code Online (Sandbox Code Playgroud)
# WRONG - no such variable LATESTVERSION_src
CLASSPATH=hibernate-$LATESTVERSION_src.zip:hibernate_$LATEST_VERSION.jar
# RIGHT
CLASSPATH=hibernate-${LATESTVERSION}_src.zip:hibernate_$LATEST_VERSION.jar
Run Code Online (Sandbox Code Playgroud)
(弗雷德的回答已经说明了这一点,但他的例子有点过于抽象)