I have a systemd unit with spaces in an argument
ExecStart=command --argument=\"text text\"
It seems that systemd does not recognize the do
I think recent versions of systemd have started accepting quotes in the middle of arguments, closer to what bash accepts. However, @Tgr's answer is still correct, and it's worth elaborating on. Quoting the entire argument, including the flag name, works here. If you do this:
ExecStart=command "--argument=text text"
Then systemd will understand --argument=text text
as a single positional argument. You don't have to worry about any more splitting happening on that space. You can see the same behavior in bash:
$ echo "--silly-flag=spaces are preserved here"
--silly-flag=spaces are preserved here
Environment is a way to do it.
You can also use \s
as space, so ExecStart will be:
ExecStart=command --argument="text=\stext"
ref: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Command%20lines
Try escaping the space, like so:
ExecStart=command --argument="text\ text"
(The quotes may or may not be necessary.)
Systemd service file supports this
Environment="TEST=one word"
Environment="TEST2=second word"
ExecStartPre=-/bin/echo start pre
ExecStartPre=/bin/echo start pre mandatory
ExecStart=/bin/echo started : ${TEST} $TEST2
ExecStartPost=-/bin/echo start post
ExecStartPost=/bin/echo start post mandatory
ExecStop=/bin/echo stop
ExecStopPost=-/bin/echo stop post
ExecStopPost=/bin/echo stop post mandatory
ExecReload=/bin/echo reload
log :
Mar 09 21:39:47 gitlab-runner-1 echo[30286]: start pre
Mar 09 21:39:47 gitlab-runner-1 echo[30288]: start pre mandatory
Mar 09 21:39:47 gitlab-runner-1 echo[30295]: started : one word second word
Mar 09 21:39:47 gitlab-runner-1 echo[30296]: start post
Mar 09 21:39:47 gitlab-runner-1 echo[30297]: start post mandatory
Mar 09 21:39:47 gitlab-runner-1 echo[30298]: stop
Mar 09 21:39:47 gitlab-runner-1 echo[30299]: stop post
Mar 09 21:39:47 gitlab-runner-1 echo[30300]: stop post mandatory
But you may actually want to set this if the app needs to read the whole string as 2 arguments, each argument between "" (not tested)
ExecStart=command "$ARG1" "$ARG2"
As Nico suggested, you can create an EvironmentFile in which you can specify an argument with spaces.
SPACEYARG="i love spaces"
In your unit file however, you'll need to wrap that argument in curly brackets in order for the spaces to be passed properly.
EnvironmentFile=/etc/.progconf
ExecStart = command ${SPACEYARG}
systemd only seems to recognize quotes which fully wrap arguments; i.e.
ExecStart=command "--argument=text text"
works but
ExecStart=command --argument="text text"
does not. I just ran into this issue and filed #624 about it.