如何在 Mac 上的 $PATH 中添加/usr/local/bin [英] How to add /usr/local/bin in $PATH on Mac
问题描述
当我在终端中打开 .profile"时,我有以下内容:
When I do 'open .profile' in the terminal I have the following:
export PATH=$PATH:/usr/local/git/bin
现在我为 Mac 安装了 node.js,它说,
Now I installed node.js for Mac and it says,
确保/usr/local/bin 在您的 $PATH 中.
Make sure that /usr/local/bin is in your $PATH.
如何添加/usr/local/bin
导出PATH=$PATH:/usr/local/git/bin
?
推荐答案
PATH
变量保存了一个以冒号分隔的目录列表,所以如果你想添加多个目录,只需输入一个它们之间的冒号:
The PATH
variable holds a list of directories separated by colons, so if you want to add more than one directory, just put a colon between them:
export PATH=$PATH:/usr/local/git/bin:/usr/local/bin
该语法适用于任何与 Bourne 兼容的 shell(sh、ksh、bash、zsh...).但是 zsh 是最近版本的 MacOS 中的默认 shell,它也以另一种方式公开了 PATH - 作为一个名为(小写)$path
的变量,它是一个数组而不是单个字符串.所以你可以这样做:
That syntax works in any Bourne-compatible shell (sh, ksh, bash, zsh...). But zsh, which is the default shell in recent versions of MacOS, also exposes the PATH another way - as a variable named (lowercase) $path
, which is an array instead of a single string. So you can do this instead:
path+=(/usr/local/git/bin /usr/local/bin)
在任何一种情况下,您都可能需要在添加目录之前检查以确保该目录不在 PATH 中.下面是使用通用语法的样子:
In either case, you may want to check to make sure the directory isn't already in the PATH before adding it. Here's what that looks like using the generic syntax:
for dir in /usr/local/git/bin /usr/local/bin; do
case "$PATH" in
$dir:*|*:$dir:*|*:$dir) :;; # already there, do nothing
*) PATH=$PATH:$dir # otherwise add it
esac
done
这是一个 zsh 特定的版本:
And here's a zsh-specific version:
for dir in /usr/local/git/bin /usr/local/bin; do
if (( ${path[(i)$dir]} > $#path )); then
path+=($dir)
fi
done
这篇关于如何在 Mac 上的 $PATH 中添加/usr/local/bin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!