如何在systemd命令行参数中使用空格?

Wol*_*ang 13 systemd

我有一个带参数空格的systemd单元

ExecStart=command --argument="text text"
Run Code Online (Sandbox Code Playgroud)

似乎systemd无法识别双引号或单引号,它将参数拆分为两个参数.知道如何防止这种情况吗?我在CoreOS中使用systemd v218.

Tgr*_*Tgr 15

systemd似乎只识别完全包装参数的引号; 即

ExecStart=command "--argument=text text"

工作,但

ExecStart=command --argument="text text"

才不是.我刚刚遇到这个问题并提交了#624.


nic*_*ico 12

不幸的是,这实际上非常难以做到.我从这个答案中窃取了这些信息.唯一的方法是将您的参数放在一个环境文件中,然后将它们用作变量(如在/etc/.progconfig中):

ARG1=text
ARG2=text
Run Code Online (Sandbox Code Playgroud)

然后在运行命令之前导入环境文件:

EnvironmentFile=/etc/.progconf
ExecStart = command $ARG1 $ARG2
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,我现在正在使用`Environment ='ARGUMENT = text text'`和`ExecStart = command --argument = $ ARGUMENT`并且有效! (7认同)

小智 6

正如 Nico 建议的那样,您可以创建一个 EvironmentFile,在其中可以指定带空格的参数。

SPACEYARG="i love spaces"
Run Code Online (Sandbox Code Playgroud)

但是,在您的单元文件中,您需要将该参数括在大括号中,以便正确传递空格。

EnvironmentFile=/etc/.progconf
ExecStart = command ${SPACEYARG}
Run Code Online (Sandbox Code Playgroud)


Jac*_*nor 5

我认为最近版本的 systemd 已经开始在参数中间接受引号,更接近 bash 接受的。但是,@Tgr 的答案仍然是正确的,值得详细说明。在这里引用整个参数,包括标志名称。如果你这样做:

ExecStart=command "--argument=text text"
Run Code Online (Sandbox Code Playgroud)

然后 systemd 将理解--argument=text text为单个位置参数。您不必担心在该空间上发生更多分裂。您可以在 bash 中看到相同的行为:

$ echo "--silly-flag=spaces     are    preserved here"
--silly-flag=spaces     are    preserved here
Run Code Online (Sandbox Code Playgroud)