这可能是一个非常基本的问题,但由于某种原因,我似乎看起来很明显.
我有一个名为的变量 filepath=/tmp/name
要访问变量,我知道我可以这样做 $filepath
在我的shell脚本中,我试图做这样的事情(后面的刻度是有意的)
`tail -1 $filepath_newstap.sh`
Run Code Online (Sandbox Code Playgroud)
这行失败了,duuh!因为没有调用变量$filepath_newstap.sh
如何附加_newstap.sh
到变量名称?请注意,后面的刻度表用于表达式评估
cho*_*oba 162
使用
"$filepath"_newstap.sh
Run Code Online (Sandbox Code Playgroud)
要么
${filepath}_newstap.sh
Run Code Online (Sandbox Code Playgroud)
要么
$filepath\_newstap.sh
Run Code Online (Sandbox Code Playgroud)
_
是标识符中的有效字符.点不是,所以shell试图插值$filepath_newstap
.
set -u
引用未定义的变量时,可以使用shell退出时出错.