bash PHP exec $PATH 变量缺少元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3428647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP exec $PATH variable missing elements
提问by Eric Cope
When I echo $PATH on my command line, it returns
当我在命令行上回显 $PATH 时,它返回
/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
当我执行这个 php 代码时
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 anyone tell me why my last two path elements are missing?
这是在 Mac OS X 上。谁能告诉我为什么我的最后两个路径元素丢失了?
采纳答案by CuriousRabbit
Environment variables on Mac OS X are set by differing mechanisms depending on how your code, or its parent process, was launched. To insure that items launched from an interactive shell and items launched by the WindowServer have the same path, you need to keep ~/.MacOSX/environment.plist in sync with what is set in .profile (or .cshrc).
Mac OS X 上的环境变量由不同的机制设置,具体取决于您的代码或其父进程的启动方式。为了确保从交互式 shell 启动的项目和由 WindowServer 启动的项目具有相同的路径,您需要使 ~/.MacOSX/environment.plist 与 .profile(或 .cshrc)中的设置保持同步。
回答by karim79
Try executing this before you call exec:
在调用之前尝试执行此操作exec:
putenv("PATH=" .$_ENV["PATH"]. ':/usr/local/git/bin:/usr/X11/bin');
回答by Paused until further notice.
What does:
有什么作用:
php -r 'print getenv("PATH");'
give you?
给你?
It's likely the shell that PHP spawns (probably shinstead of bash) isn't getting the same environment that you have at the command line. You don't say how you're running your execcommand.
PHP 生成的 shell(可能sh而不是bash)可能没有获得与您在命令行中拥有的环境相同的环境。你没有说你如何运行你的exec命令。
This will show you which shell is being run:
这将显示正在运行的 shell:
php -r 'echo shell_exec("echo ##代码##");'
You may need to use the putenvcommand or determine whether your path needs to be set in /etc/profile, ~/.profileor ~/.bashrcin order for it to be picked up.
您可能需要使用该putenv命令或确定您的路径是否需要在 中设置/etc/profile,~/.profile或~/.bashrc以便被选取。

