Missing PHP variable elements exec $ PATH

When I return $ PATH to my command line, it returns

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/MAMP/Library/bin:/usr/local/git/bin:/usr/X11/bin 

When I execute this php code

 exec('echo $PATH; whoami; less /etc/paths; 2>&1') 

I get

 string 'echo $PATH; whoami; less /etc/paths; 2>&1' (length=56) array 0 => string '/usr/bin:/bin:/usr/sbin:/sbin' (length=29) 1 => string 'eric' (length=4) 2 => string '/usr/bin' (length=8) 3 => string '/bin' (length=4) 4 => string '/usr/sbin' (length=9) 5 => string '/sbin' (length=5) 6 => string '/usr/local/bin' (length=14) 7 => string '/Applications/MAMP/Library/bin' (length=30) 8 => string '/usr/bin:/bin:/usr/sbin:/sbin' (length=29) 

This is on Mac OS X. Can someone tell me why my last two path elements are missing?

+6
bash php path environment-variables exec
source share
3 answers

Environment variables in Mac OS X are set by various mechanisms, depending on how your code or its parent process was run. To ensure that elements launched from the interactive shell and elements launched by WindowServer have the same path, you need to synchronize ~ / .MacOSX / environment.plist with what is set in .profile (or .cshrc).

+2
source share

Try this before you call exec :

 putenv("PATH=" .$_ENV["PATH"]. ':/usr/local/git/bin:/usr/X11/bin'); 
+10
source share

What is he doing:

 php -r 'print getenv("PATH");' 

to give you?

Most likely, the shell created by PHP (perhaps sh instead of bash ) does not get the same environment as on the command line. You do not say how you use the exec command.

This will show you which shell is starting:

 php -r 'echo shell_exec("echo $0");' 

You may need to use the putenv command or determine if your path must be set to /etc/profile , ~/.profile or ~/.bashrc so that it can be raised.

+5
source share

All Articles