diff --git a/Library/Homebrew/brew.sh b/Library/Homebrew/brew.sh index 63dac99fb1..b53a7750f2 100644 --- a/Library/Homebrew/brew.sh +++ b/Library/Homebrew/brew.sh @@ -51,23 +51,8 @@ HOMEBREW_CACHE="${HOMEBREW_CACHE:-${HOMEBREW_DEFAULT_CACHE}}" HOMEBREW_LOGS="${HOMEBREW_LOGS:-${HOMEBREW_DEFAULT_LOGS}}" HOMEBREW_TEMP="${HOMEBREW_TEMP:-${HOMEBREW_DEFAULT_TEMP}}" -# Don't need to handle a default case. -# HOMEBREW_LIBRARY set by bin/brew -# shellcheck disable=SC2249,SC2154 -case "$*" in - --cellar) echo "${HOMEBREW_CELLAR}"; exit 0 ;; - --repository|--repo) echo "${HOMEBREW_REPOSITORY}"; exit 0 ;; - --caskroom) echo "${HOMEBREW_PREFIX}/Caskroom"; exit 0 ;; - --cache) echo "${HOMEBREW_CACHE}"; exit 0 ;; - shellenv) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh"; homebrew-shellenv; exit 0 ;; - formulae) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/formulae.sh"; homebrew-formulae; exit 0 ;; - casks) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/casks.sh"; homebrew-casks; exit 0 ;; - # falls back to cmd/prefix.rb on a non-zero return - --prefix*) source "${HOMEBREW_LIBRARY}/Homebrew/prefix.sh"; homebrew-prefix "$@" && exit 0 ;; -esac - ##### -##### Next, define all helper functions. +##### Next, define helper functions for prompting. ##### # These variables are set from the user environment. @@ -125,6 +110,25 @@ odie() { exit 1 } +# Don't need to handle a default case. +# HOMEBREW_LIBRARY set by bin/brew +# shellcheck disable=SC2249,SC2154 +case "$*" in + --cellar) echo "${HOMEBREW_CELLAR}"; exit 0 ;; + --repository|--repo) echo "${HOMEBREW_REPOSITORY}"; exit 0 ;; + --caskroom) echo "${HOMEBREW_PREFIX}/Caskroom"; exit 0 ;; + --cache) echo "${HOMEBREW_CACHE}"; exit 0 ;; + shellenv) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/shellenv.sh"; homebrew-shellenv; exit 0 ;; + formulae) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/formulae.sh"; homebrew-formulae; exit 0 ;; + casks) source "${HOMEBREW_LIBRARY}/Homebrew/cmd/casks.sh"; homebrew-casks; exit 0 ;; + # falls back to cmd/prefix.rb on a non-zero return + --prefix*) source "${HOMEBREW_LIBRARY}/Homebrew/prefix.sh"; homebrew-prefix "$@" && exit 0 ;; +esac + +##### +##### Next, define the reset of helper functions. +##### + safe_cd() { cd "$@" >/dev/null || odie "Failed to cd to $*!" } diff --git a/Library/Homebrew/cmd/shellenv.sh b/Library/Homebrew/cmd/shellenv.sh index 2434efee15..03531b75be 100644 --- a/Library/Homebrew/cmd/shellenv.sh +++ b/Library/Homebrew/cmd/shellenv.sh @@ -4,13 +4,24 @@ #: #: The variables `HOMEBREW_PREFIX`, `HOMEBREW_CELLAR` and `HOMEBREW_REPOSITORY` are also exported to avoid querying them multiple times. #: The variable `HOMEBREW_SHELLENV_PREFIX` will be exported to avoid adding duplicate entries to the environment variables. -#: Consider adding evaluation of this command's output to your dotfiles (e.g. `~/.profile`, `~/.bash_profile`, or `~/.zprofile`) with: `eval $(brew shellenv)` +#: +#: Consider adding evaluation of this command's output to your dotfiles (e.g. `~/.profile`, `~/.bash_profile`, or `~/.zprofile`) +#: with: `eval "$(/path/to/brew shellenv)"`. +#: +#: The variable `HOMEBREW_SHELLENV_PREFIX` may be inherited from the parent shell session. If you explicitly +#: modified the path variables (e.g. `PATH`) in your dotfiles which breaks the subshell varibale inheritecement, +#: please use `eval "$(HOMEBREW_SHELLENV_PREFIX='' /path/to/brew shellenv)"` to always get all the export statements. # HOMEBREW_CELLAR and HOMEBREW_PREFIX are set by extend/ENV/super.rb # HOMEBREW_REPOSITORY is set by bin/brew # shellcheck disable=SC2154 homebrew-shellenv() { - [[ "${HOMEBREW_SHELLENV_PREFIX}" == "${HOMEBREW_PREFIX}" ]] && return + if [[ "${HOMEBREW_SHELLENV_PREFIX}" == "${HOMEBREW_PREFIX}" ]]; then + [[ "$(PATH="${HOMEBREW_PATH}" command -v brew)" == "${HOMEBREW_PREFIX}/bin/brew" ]] && return + opoo "You have set HOMEBREW_SHELLENV_PREFIX=\"${HOMEBREW_SHELLENV_PREFIX}\"," \ + "but the brew's executable does not present in your PATH." \ + "Please run \`brew help shellenv\` for more information." + fi case "$(/bin/ps -p "${PPID}" -c -o comm=)" in fish | -fish) diff --git a/docs/Manpage.md b/docs/Manpage.md index 12b9f20c17..63a494ebb8 100644 --- a/docs/Manpage.md +++ b/docs/Manpage.md @@ -570,7 +570,13 @@ Print export statements. When run in a shell, this installation of Homebrew will The variables `HOMEBREW_PREFIX`, `HOMEBREW_CELLAR` and `HOMEBREW_REPOSITORY` are also exported to avoid querying them multiple times. The variable `HOMEBREW_SHELLENV_PREFIX` will be exported to avoid adding duplicate entries to the environment variables. -Consider adding evaluation of this command's output to your dotfiles (e.g. `~/.profile`, `~/.bash_profile`, or `~/.zprofile`) with: `eval $(brew shellenv)` + +Consider adding evaluation of this command's output to your dotfiles (e.g. `~/.profile`, `~/.bash_profile`, or `~/.zprofile`) +with: `eval "$(/path/to/brew shellenv)"`. + +The variable `HOMEBREW_SHELLENV_PREFIX` may be inherited from the parent shell session. If you explicitly +modified the path variables (e.g. `PATH`) in your dotfiles which breaks the subshell varibale inheritecement, +please use `eval "$(HOMEBREW_SHELLENV_PREFIX='' /path/to/brew shellenv)"` to always get all the export statements. ### `tap` [*`options`*] [*`user`*`/`*`repo`*] [*`URL`*] diff --git a/manpages/brew.1 b/manpages/brew.1 index 3586e59ecf..9a16049cfc 100644 --- a/manpages/brew.1 +++ b/manpages/brew.1 @@ -792,7 +792,13 @@ Search for \fItext\fR in the given database\. Print export statements\. When run in a shell, this installation of Homebrew will be added to your \fBPATH\fR, \fBMANPATH\fR, and \fBINFOPATH\fR\. . .P -The variables \fBHOMEBREW_PREFIX\fR, \fBHOMEBREW_CELLAR\fR and \fBHOMEBREW_REPOSITORY\fR are also exported to avoid querying them multiple times\. The variable \fBHOMEBREW_SHELLENV_PREFIX\fR will be exported to avoid adding duplicate entries to the environment variables\. Consider adding evaluation of this command\'s output to your dotfiles (e\.g\. \fB~/\.profile\fR, \fB~/\.bash_profile\fR, or \fB~/\.zprofile\fR) with: \fBeval $(brew shellenv)\fR +The variables \fBHOMEBREW_PREFIX\fR, \fBHOMEBREW_CELLAR\fR and \fBHOMEBREW_REPOSITORY\fR are also exported to avoid querying them multiple times\. The variable \fBHOMEBREW_SHELLENV_PREFIX\fR will be exported to avoid adding duplicate entries to the environment variables\. +. +.P +Consider adding evaluation of this command\'s output to your dotfiles (e\.g\. \fB~/\.profile\fR, \fB~/\.bash_profile\fR, or \fB~/\.zprofile\fR) with: \fBeval "$(/path/to/brew shellenv)"\fR\. +. +.P +The variable \fBHOMEBREW_SHELLENV_PREFIX\fR may be inherited from the parent shell session\. If you explicitly modified the path variables (e\.g\. \fBPATH\fR) in your dotfiles which breaks the subshell varibale inheritecement, please use \fBeval "$(HOMEBREW_SHELLENV_PREFIX=\'\' /path/to/brew shellenv)"\fR to always get all the export statements\. . .SS "\fBtap\fR [\fIoptions\fR] [\fIuser\fR\fB/\fR\fIrepo\fR] [\fIURL\fR]" Tap a formula repository\.