解释

解释

我已经使用以下方式安装了 Homebrew:

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

为什么brew找不到该命令?

(base) rags@Rageshs-Mac-mini ~ % brew help
zsh: command not found: brew

我的$PATH变量根据外壳:

(base) rags@Rageshs-Mac-mini ~ % echo $path
/Users/rags/opt/anaconda3/bin /Users/rags/opt/anaconda3/condabin /usr/local/bin /usr/bin /bin /usr/sbin /sbin /Library/Apple/usr/bin

答案1

Homebrew 未包含在您的$PATH变量中,这是导致 shell 找不到命令的原因brew

要修复此问题,您必须编辑 shell 启动脚本.zshrc。Homebrew 安装程序不会进行此编辑,而是指示用户执行此操作,您可能没有注意到这一点。

添加以下行到.zshrc

eval $(/opt/homebrew/bin/brew shellenv)

您可以使用文本编辑器或执行 shell 命令来添加它:

$ echo 'eval $(/opt/homebrew/bin/brew shellenv)' >> $HOME/.zshrc

现在,通过打开一个新的 shell 窗口重新加载 shell,就可以开始了。


解释

.zshrc每次打开新 shell 时都会执行中的行。

当炮弹到达线时

eval $(/opt/homebrew/bin/brew shellenv)

它将首先执行

/opt/homebrew/bin/brew shellenv

即执行brew给定其完整路径的二进制文件,并以shellenvprovided 作为参数。来自man brew

    shellenv
        Print export statements. When run in  a  shell,  this  installation  of
        Homebrew will be added to your PATH, MANPATH, and INFOPATH.

        The  variables HOMEBREW_PREFIX, HOMEBREW_CELLAR and HOMEBREW_REPOSITORY
        are also exported to  avoid  querying  them  multiple  times.  Consider
        adding  evaluation  of  this  command's  output  to your dotfiles (e.g.
        ~/.profile, ~/.bash_profile, or ~/.zprofile) with:  eval  $(brew  shel-
        lenv)

确实,输出brew shellenv是:

$ brew shellenv
export HOMEBREW_PREFIX="/opt/homebrew";
export HOMEBREW_CELLAR="/opt/homebrew/Cellar";
export HOMEBREW_REPOSITORY="/opt/homebrew";
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin${PATH+:$PATH}";
export MANPATH="/opt/homebrew/share/man${MANPATH+:$MANPATH}:";
export INFOPATH="/opt/homebrew/share/info:${INFOPATH:-}";

因此实际上,shell 启动脚本执行eval $(...),并由...上面的行替换。

相关内容