16

I have a systemd unit with spaces in an argument

ExecStart=command --argument="text text"

It seems that systemd does not recognize the double or single quotes and it splits up the argument into two arguments. Any idea how I can prevent that? I am using systemd v218 within CoreOS.

2

7 Answers 7

18

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I just came across this problem and it costs me several hours to figure out
With systemd-246 even wrapping the whole arguments with double quotes doesn't work. In my case the command is /my/path/mycommand "--myargs". I had to fall back to the solution provided by @Niki that is use an environment variable.
14

This is actually surprisingly difficult to do, unfortunately. I stole this info from this answer. The only way to do it is to put your arguments in an environment file and then use them as variables as such (like in /etc/.progconfig):

ARG1=text
ARG2=text

Then import the environment file before running your command:

EnvironmentFile=/etc/.progconf
ExecStart = command $ARG1 $ARG2

3 Comments

Thanks, I am now using Environment='ARGUMENT=text text' and ExecStart = command --argument=$ARGUMENT and that works!
Can you explain more briefly how have you achieved it?
Judging by the amount of upvotes, this may have worked at some point. But it does not work anymore, at least in RHEL 7 (which is fairly old at this point). The variables need to be passed with braces as shown in @Niki's answer.
6

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}

1 Comment

@nico's solution did not work for me. Neither did using double quotes around the variable name. Braces do work though. Thanks.
5

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

Comments

3

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

Comments

1

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"

Comments

-3

Try escaping the space, like so:

ExecStart=command --argument="text\ text"

(The quotes may or may not be necessary.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.