I'm trying to set the PATH
environment variable in PHP and am encountering some unexpected behaviour:
var_dump(getenv('PATH'));
system('echo $PATH');
system('which true 2>&1');
system('PATH="$PATH" which true 2>&1');
results in
string(23) "/usr/local/bin:/usr/bin"
/usr/local/bin:/usr/bin
which: no true in ((null))
/usr/bin/true
This is also mirrored by the chosen executables which are from /usr/bin
rather than /usr/local/bin
.
My goal is (obviously) to change the path of some executable called during the script. But I can neither directly change the path of the executable whose path I want to change, because it is called indirectly by some binary. Nor can I simply use the PATH="$PATH"
variant without patching an external library.
Because the obvious workarounds aren't applicable, and because I find this very curious, I'd like an explanation why this happens and how (or if) it is possible to set the PATH in a way that is passed down to system
or exec
calls.
The script is run via php-fpm, in case that's of interest here.