PHP exec $PATH 变量缺少元素

2022-01-02 00:00:00 path environment-variables bash exec php

当我在命令行上回显 $PATH 时,它返回

When I echo $PATH on 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

当我执行这段 php 代码时

When I execute this php code

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

我明白

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)

这是在 Mac OS X 上.谁能告诉我为什么我的最后两个路径元素丢失了?

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

推荐答案

Mac OS X 上的环境变量由不同的机制设置,具体取决于您的代码或其父进程的启动方式.为了确保从交互式 shell 启动的项目和由 WindowServer 启动的项目具有相同的路径,您需要使 ~/.MacOSX/environment.plist 与 .profile(或 .cshrc)中的设置保持同步.

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).

相关文章