bash :替换""内的变量值 [英] bash : replace variable value inside ' '
问题描述
对不起,如果问题很简单,但我是 shell 脚本的新手.我正在尝试写这样的东西:
for i in {1..20}做curl "某物 $i ........ -d '某物 "$i" 某物' "完毕
问题是单引号部分 '' 中的第二个 $i
没有被替换.应该怎么做才能让它工作?
如上所说,参数在单引号内不展开,必须使用双引号.唯一的一点是,因为它出现在一个已经被双引号括起来的字符串中,你必须用反斜杠 (\
) 将它们转义,就像这样:
注意在最里面的 "
之前有三个 \
,因为这将被扩展两次(在评估 eval
的参数时一次,并且评估 echo
) 的参数时一次
Sorry if the question is very straight forward but am a newbie to shell scripting. I am trying to write something like this :
for i in {1..20}
do
curl "something $i ........ -d 'something "$i" something' "
done
The problem is that the second $i
inside the single quotes part '' is not being replaced. What should be done to get it working ?
As said above, parameters are not expanded inside single quotes, you have to use double quotes. The only point is that since it occurs in a already double-quoted string, you have to escape them with a backslash (\
), like this:
$ foo=bar $ eval "echo \"something \\\"$foo\\\"\"" something "bar"
Note that there are three \
before the innermost "
, as this will be expanded twice (once when evaluating the argument of eval
and once when evaluating the argument of echo
)
这篇关于bash :替换""内的变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!