1

I'd like to export all variables used by a certain systemd service, and are specified in its unit file using EnvironmentFile.

E.g. foo.service :

[Service]
...
EnvronmentFile=/foo.env
EnvronmentFile=/bar.env

foo.env:

a=1
#b=2
c=3

So I've thought adding to my bashrc something like

set -a
grep EnvironmentFile /etc/systemd/system/foo.service | grep -v '^ *#' | cut -d'=' -f2 | xargs  -I {} bash -c 'source {};'
set +a

as specified in this answer.

Is there a more elegant solution?

1

This won't work at all because you're executing a new bash shell to do the 'source'

Try:

load_env() 
{
  local files=( $(egrep '^[ ]*EnvironmentFile' "$1" ) )
  local f 
  set -a
  for f in "${files[@]}"
  do
     .   "${f##*=}"   # this expression delete the EnvironmentFile= part
  done
  set +a
}

load_env /etc/systemd/system/foo.service

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.