In addition to what the accepted answer says, if you put your environment variables into a drop-in file, then you need to use the following syntax to make sure they are not all treated as one argument to your program:
env.conf drop-in file
[Service]
Environment='CATALINA_OPTS=-Dappserver.home=/var/lib/archiva/apache-tomcat-current -Dappserver.base=/var/lib/archiva/apache-tomcat-current'
unit.service
[Unit]
Description=My Daemon
[Service]
ExecStart=/bin/echo $CATALINA_OPTS
[Install]
WantedBy=multi-user.target
I don't know why using just $ENV_NAME
worked for me, and ${ENV_NAME}
didn't, and I couldn't find anything documenting this difference. All I can say is that $ENV_NAME
syntax worked to treat each argument in the variable which is separated by space, as distinct arguments, whereas the ${ENV_NAME}
, treated them all as one argument.
Maybe using echo
is not the best way to tell this difference. I suggest anyone looking to test it to use something like /usr/bin/printf "%s\n" ${ENV_NAME}
vs /usr/bin/printf "%s\n" $ENV_NAME
, and see what they get.
Creating drop-in files
One can create a drop-in file by using the edit
command for systemctl. For example in the case of this question, the command would be:
sudo systemctl edit archiva.service
You can also create one like I did above by adding a custom file to /lib/systemd/system/archiva.service.d/
folder
EDIT
I found the documentation on observed environmental variable behavior, thanks to another answer:
Basic environment variable substitution is supported. Use "${FOO}" as
part of a word, or as a word of its own, on the command line, in which
case it will be erased and replaced by the exact value of the
environment variable (if any) including all whitespace it contains,
always resulting in exactly a single argument. 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.