在shell变量插值 [英] variable interpolation in shell
问题描述
这可能是一个很基本的问题,但由于某种原因,我似乎在俯瞰显而易见的。
This is probably a very basic question but for some reason I seem to be over looking the obvious.
我有一个名为变量文件路径= / tmp目录/名称
要访问我知道我能做到这一点变量 $文件路径
To access the variable I know that I can do this $filepath
在我的shell脚本我试图做这样的事情(背面蜱意)
In my shell script I attempted to do something like this (The back ticks are intended)
`tail -1 $filepath_newstap.sh`
这个线路出现故障时,duuh!因为变量不叫 $ filepath_newstap.sh
This line fails, duuh! because the variable is not called $filepath_newstap.sh
我如何追加 _newstap.sh
来的变量名?请注意,反引号旨在为前pression评价
How do I append _newstap.sh
to the variable name? Please note that back ticks are intended for the expression evaluation
推荐答案
使用
"$filepath"_newstap.sh
或
${filepath}_newstap.sh
或
$filepath\_newstap.sh
_
是标识符有效的字符。点是没有,所以壳试图插 $ filepath_newstap
。
_
is a valid character in identifiers. Dot is not, so the shell tried to interpolate $filepath_newstap
.
您可以使用设置-u
,使外壳的出口与一个错误,当你引用一个未定义的变量。
You can use set -u
to make the shell exit with an error when you reference an undefined variable.
这篇关于在shell变量插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!