不完整的 PATH 变量
Incomplete PATH variable
我试图在 OSX 上检索 Mono 中 PATH
变量的值。但是,我没有从终端获得相同的结果。
在 C# 中(不完整):
Environment.GetEnvironmentVariable("PATH")
/usr/bin:/bin:/usr/sbin:/sbin
在终端中(预期):
echo $PATH
/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:.:~/.composer/vendor/bin:/usr/local/php5/bin:/opt/X11/bin:/usr/local/share/dotnet:/Library/Frameworks/Mono.framework/Versions/Current/Commands
我尝试使用每个可用的 EnvironmentVariableTarget
作为 GetEnvironmentVariable
的第二个参数,但 none return 完整路径。
我是不是遗漏了什么明显的东西?
启动新终端时 window,shell 自动执行 /etc/profile
和 /etc/bashrc_Apple_Terminal
(或不使用终端时 /etc/bashrc
)。
/etc/profile
的一部分是 运行 /usr/libexec/path_helper -s
,它构建了一个路径命令,除了您还需要的 "default" 元素之外,还向您的路径添加了额外的元素使用 Environment.GetEnvironmentVariable("Path")
时查看。
要获取 shell 使用的确切路径的通用解决方案,您可以使用以下代码:
var info = new ProcessStartInfo();
info.FileName = "/bin/bash";
info.Arguments = "-l -c \"echo $PATH\""; // -l = 'login shell' so we execute /etc/profile
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
var p = Process.Start(info);
p.WaitForExit();
string path = p.StandardOutput.ReadToEnd().Trim(); // Drop the trailing \n from our echo output
然而,如果您只是想要特定于单声道的路径,您可以只读取文件的内容/etc/paths.d/mono-commands
。这就是 shell 通过 path_helper
获取添加到 Path 变量的单声道路径的地方。
我试图在 OSX 上检索 Mono 中 PATH
变量的值。但是,我没有从终端获得相同的结果。
在 C# 中(不完整):
Environment.GetEnvironmentVariable("PATH")
/usr/bin:/bin:/usr/sbin:/sbin
在终端中(预期):
echo $PATH
/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:.:~/.composer/vendor/bin:/usr/local/php5/bin:/opt/X11/bin:/usr/local/share/dotnet:/Library/Frameworks/Mono.framework/Versions/Current/Commands
我尝试使用每个可用的 EnvironmentVariableTarget
作为 GetEnvironmentVariable
的第二个参数,但 none return 完整路径。
我是不是遗漏了什么明显的东西?
启动新终端时 window,shell 自动执行 /etc/profile
和 /etc/bashrc_Apple_Terminal
(或不使用终端时 /etc/bashrc
)。
/etc/profile
的一部分是 运行 /usr/libexec/path_helper -s
,它构建了一个路径命令,除了您还需要的 "default" 元素之外,还向您的路径添加了额外的元素使用 Environment.GetEnvironmentVariable("Path")
时查看。
要获取 shell 使用的确切路径的通用解决方案,您可以使用以下代码:
var info = new ProcessStartInfo();
info.FileName = "/bin/bash";
info.Arguments = "-l -c \"echo $PATH\""; // -l = 'login shell' so we execute /etc/profile
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
var p = Process.Start(info);
p.WaitForExit();
string path = p.StandardOutput.ReadToEnd().Trim(); // Drop the trailing \n from our echo output
然而,如果您只是想要特定于单声道的路径,您可以只读取文件的内容/etc/paths.d/mono-commands
。这就是 shell 通过 path_helper
获取添加到 Path 变量的单声道路径的地方。