I try to write a service to set up the network interface.
In Sysvinit I used a script:

IP=$(fw_printenv -n ipaddr)
ifconfig eth0 $IP netmask up

Now I want to do this in a Service:

Environment="IP=$(/sbin/fw_printenv -n ipaddr)"
ExecStart=/sbin/ifconfig eth0 $IP netmask up

This does not work. In systemctl status -l start-network.service it says:

Oct 23 06:17:29 lcd5a-dsp ifconfig[261]: ifconfig: invalid number 'sbin/fw_printenv'

Does anybody know how to use the output of a function as a variable in a systemd.service?


Note that Environment="var=$(cmd arg)" does not actually evaluate the $() part. The variable is just set to the string "$(cmd arg)".

Also, ExecStart=... $var ... will replace $var by the string, but will also split the resulting line on spaces, so you will effectively get "$(cmd" and "arg)" in the result. You need to use ${var} to avoid this and keep the string as one arg.

However, this isn't going to work, as nothing is going to evaluate the $(), so you need to explicitly call bash -c and pass the whole command as a single arg by enclosing it in quotes. These quotes are not like bash quotes and do not stop the expansion of ${var} (though it does stop $var expanding if it is not on its own). So finally, this should work:

ExecStart=/bin/bash -c '/sbin/ifconfig eth0 ${IP} netmask up'

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.