variablesenvironment-variablessystemd

Expand multiple words in environment variable using systemd


I have a problem using systemd's environment files: Defining a variable like

#... many more
CHECK_SELECTION=--no-check-a --no-check-b --no-check-c
#... many more

in an environment file for a systemd unit, I have a problem:

  1. When specifying $CHECK_SELECTION, then it is passed literally in systemd 228
  2. When specifying ${CHECK_SELECTION}, then the three words are passed as one parameter, causing a syntax error.

The manual page systemd.service(5) however claims that it should work (as I read it):

       Use "$FOO" as a separate word on the command line, in which case it
       will be replaced by the value of the environment variable split at
       whitespace, resulting in zero or more arguments. For this type of
       expansion, quotes are respected when splitting into words, and
       afterwards removed.

My ExecStart looks like this:

ExecStart=/usr/bin/my-prog \
--many-more-options \
${CHECK_SELECTION} --notification-template=${NOTIFICATION_TEMPLATE} \
${OTHER_OPTIONS}

So what is wrong? Could it be I found a bug in systemd (as the whitespace is from the line continued)?


Solution

  • Problem solved (was caused by confused user, actually):

    I had started using $VAR syntax for an option like --option=$VAR, and when realizing that the value wasn't substituted (and the program aborted with syntax error), I replaced all variable occurrences with ${VAR}.

    As per documentation ${VAR} won't split words, however.

    So the solution is to use ${VAR} where there is no space around and no word-splitting is needed or desired, and to use $VAR where word-splitting is required (and where space is around to enable the substitution).