Problem
Bash can only accept key/value arguments local to the command as long as they are separate pairs and not a full string containing all the key/value pairs concatenated together,
i.e. if you have a script (test.bash
) containing
echo "$FOO"
echo "$BAR"
and doing
FOO=foo BAR=bar bash test.bash
produces
foo
bar
but the same doesn't work, if you pass those key/value pairs as a single string. i.e.
x='FOO=foo BAR=bar'
"$x" bash test.bash
cannot work, because the order of shell expansions doesn't allow that. By the time the variable expansion of x
happens, the command is evaluated with the expanded value as below. The re-processing of the command line variables doesn't happen again.
'FOO=foo BAR=bar' bash test.bash
which is not correct, as the whole literal string 'FOO=foo BAR=bar'
is treated as a one long command.
Solution
You could use the env
command directly with the -S
flag to pass each k/v pair into the environment, in which the script
is run
env -S "$(perl -I/foo -MFoo=vars -E'say vars()')" script
By doing the above, each k/v pair in is added as additional environment variables to the shell in which the script is being run. Also note that -S
is not portable (not POSIX compliant).