Using spaces in env vars during Apache startup

167 Views Asked by At

I want to pass an environment variable with a space in it to apache. Apache uses env to start itself so in effect I have to make some thing like this work

export MYCMD=" env -i FOO=123\ 456"
$MYCMD

The result is

env: 456: No such file or directory

Looking at the strace output I see this

execve("/usr/bin/env", ["env", "-i", "FOO=123\\", "456"], [/* 41 vars */]) = 0

as you can see the env utility has split up my FOO variable into two other variables because of the space. I have tried all manner of options to try and maintain the space after env has digested the command but so far no luck.

You can try this on its own

env -i FOO=123\ 456 

FOO=123 456

All is well

execve("/usr/bin/env", ["env", "-i", "FOO=123 456"],

The problem occurs when you place the command into another environment variable

Since this is what the Apache start up code does I am somewhat stuck with having to find a solution to this puzzle.

Many thanks in advance.

2

There are 2 best solutions below

0
On

The problem would seem to be that the second level of quoting (the backslash in your eample) is not evaluated by a shell.

Is there a chance you can invoke your command like

eval "$MYCMD"

instead?

0
On

I might be misunderstanding what you want, but you can try using an array instead of a simple variable.

export MYCMD=( env -i "FOO=123 456" )
"${MYCMD[@]}"

I suspect that's a little too simple for you, as "exporting" an array doesn't really work. I think bash does a little sleight-of-hand to make "exported" arrays visible to subshells, but strictly speaking an environment variable is simply a named string without any other type of structure.

How is MYCMD being used, exactly? Which file do you place it in? Changes to the environment persist, so if you were to set FOO="123 456" in the environment of the command that starts Apache, Apache would inherit FOO as well.