0

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.

Tobias Gödderz
  • 321
  • 1
  • 2
  • You're example doesn't seem consistent or reproducible. If $PATH is properly set as shown in the 2nd line, the 3rd line should be working. Are you sure lines 3 and 4 of the results aren't switched around? – Devon Bessemer May 17 '17 at 12:36
  • Yes I'm sure, that's why I'm so puzzled by this, too. – Tobias Gödderz May 18 '17 at 06:41

0 Answers0