以下测试是在CentOS 7.1中进行的。
在以下文件test.service
中创建以下文件/usr/lib/systemd/system/
[Unit]
Description=Test Bash Brace Expansion
Wants=network.target
After=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash -c "a='hello world'; echo ${a}"
[Install]
WantedBy=multi-user.target
并执行 systemctl daemon-reload; systemctl restart test; systemctl status test -l
没有值的输出,因为${a}
不会被替换为单词hello world
,直到将其更改echo ${a}
为
echo $a
:工作 echo $${a}
:工作该$$
机构在AA的bash进程的PID,但为什么可以在$$
在这样的伎俩ExecStart
来可能得到这个词hello world
?
您要做的不是括号扩展,而是参数扩展。括号扩展(例如${1..5}
,不能在双引号内使用)。
参数扩展确实在双引号内起作用,但是如果替换应在被调用的shell中而不是当前的shell中进行,则$
需要在符号上加上引号。
考虑以下示例:
a='bye world' ; /bin/bash -c "a='hello world'; echo ${a}"
bye world
与这个:
a='bye world' ; /bin/bash -c "a='hello world'; echo \${a}"
hello world
重要的是要记住,您的systemd服务描述文件不是在Bash中执行的(如上面的示例所示),因此有一些非常相似但略有不同的规则,请参阅服务单元配置文件的文档。
您的问题是,就像上面第一个示例中的交互式Bash一样,systemd试图扩展${a}
其环境中没有的功能。正如您已经注意到的,$$
是您的解决方案,上面的链接说明了原因:
要传递文字美元符号,请使用“ $$”。
这允许参数扩展发生在Bash shell systemd正在调用中。因此,配置文件中的行应显示为:
ExecStart=/bin/bash -c "a='hello world'; echo $${a}"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句