在shell脚本中,我们什么时候使用{}
扩展变量?
例如,我看到了以下内容:
var=10 # Declare variable
echo "${var}" # One use of the variable
echo "$var" # Another use of the variable
有显着差异,还是只是风格?一个比另一个更受欢迎吗?
在shell脚本中,我们什么时候使用{}
扩展变量?
例如,我看到了以下内容:
var=10 # Declare variable
echo "${var}" # One use of the variable
echo "$var" # Another use of the variable
有显着差异,还是只是风格?一个比另一个更受欢迎吗?
在这个特定的例子中,它没有任何区别。但是,如果您想扩展字符串中的变量, {}
in很有用${}
foo
"${foo}bar"
因为"$foobar"
将改为扩展由foobar
.
在以下情况下也无条件地需要花括号:
${array[42]}
${filename%.*}
(remove extension)"$8 $9 ${10} ${11}"
在任何地方都这样做,而不仅仅是在潜在的模棱两可的情况下,可以被认为是良好的编程实践。这既是为了保持一致性,也是为了避免出现意外,例如$foo_$bar.jpg
,下划线成为变量名的一部分在视觉上并不明显。
变量在没有$
和没有的情况下被声明和赋值{}
。你必须使用
var=10
分配。为了从变量中读取(换句话说,“扩展”变量),您必须使用$
.
$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.
这有时让我感到困惑——在其他语言中,我们以相同的方式引用变量,无论它是在赋值的左侧还是右侧。但是 shell 脚本是不同的,$var=10
它不会像你想象的那样做!
您{}
用于分组。大括号是取消引用数组元素所必需的。例子:
dir=(*) # store the contents of the directory into an array
echo "${dir[0]}" # get the first entry.
echo "$dir[0]" # incorrect
您还可以在大括号内进行一些文本操作:
STRING="./folder/subfolder/file.txt"
echo ${STRING} ${STRING%/*/*}
结果:
./folder/subfolder/file.txt ./folder
或者
STRING="This is a string"
echo ${STRING// /_}
结果:
This_is_a_string
你说得对,“正则变量”是不需要的……但它对调试和阅读脚本更有帮助。
访问数组元素和执行大括号扩展总是需要花括号。
即使没有歧义,也不要过度谨慎并{}
用于 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
所以,最好把这三行写成:
path=/var/$dir/$prog
logfile=$path/$prog.log
path_copy=$path
这绝对更具可读性。
由于变量名不能以数字开头,因此 shell 不需要{}
围绕编号的变量(例如$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
看:
变量名的结尾通常用空格或换行符表示。但是,如果我们在打印变量值后不想要空格或换行符怎么办?花括号告诉 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"
# 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
(弗雷德的回答已经说明了这一点,但他的例子有点太抽象了)
遵循 SierraX 和 Peter 关于文本操作的建议,大括号{}
用于将变量传递给命令,例如:
假设您有一个sposi.txt文件,其中包含一部著名的意大利小说的第一行:
> sposi="somewhere/myfolder/sposi.txt"
> cat $sposi
输出:quel ramo del lago di como che volge a mezzogiorno
现在创建两个变量:
# Search the 2nd word found in the file that "sposi" variable points to
> word=$(cat $sposi | cut -d " " -f 2)
# This variable will replace the word
> new_word="filone"
现在用sposi.txt 文件中的new_word替换word变量内容
> sed -i "s/${word}/${new_word}/g" $sposi
> cat $sposi
输出:quel filone del lago di como che volge a mezzogiorno
“ramo”一词已被替换。