【macOS】gnubin への PATH を一括で通す

【macOS】GNU パッケージの PATH
GNU gsed をインストールした後の表示。


GNU "sed" has been installed as "gsed".
If you need to use it as "sed", you can add a "gnubin" directory
to your PATH from your bashrc like:

     PATH="$HOMEBREW_PREFIX/opt/gnu-sed/libexec/gnubin:$PATH"

👉 gnu-sed — Homebrew Formulae hatena-bookmark

sed のように利用するには PATH を優先しておく、とのことだが。

GNU パッケージは、「パッケージごとに」シンボリックシンクが分かれている。


Commands also provided by macOS and the commands dir, dircolors, vdir have been installed with the prefix "g".
If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH with:
    PATH="$HOMEBREW_PREFIX/opt/coreutils/libexec/gnubin:$PATH"

👉 coreutils — Homebrew Formulae hatena-bookmark

自分のマシンで見てみる。


❯ ls -l $(brew --prefix)/opt/*/libexec/gnubin
/opt/homebrew/opt/gnu-sed/libexec/gnubin:
total 0
lrwxr-xr-x@ 1 mao  admin  14 Nov  7  2022 sed@ -> ../../bin/gsed

/opt/homebrew/opt/gsed/libexec/gnubin:
total 0
lrwxr-xr-x@ 1 mao  admin  14 Nov  7  2022 sed@ -> ../../bin/gsed

/opt/homebrew/opt/libtool/libexec/gnubin:
total 0
lrwxr-xr-x@ 1 mao  admin  18 Mar 17  2022 libtool@ -> ../../bin/glibtool
lrwxr-xr-x@ 1 mao  admin  21 Mar 17  2022 libtoolize@ -> ../../bin/glibtoolize

そうか、インストール時に作ってくれてるんだシンボリックリンク。

sed@ が2個あるけど実体は同じでした。


❯ ls -al /opt/homebrew/opt/gnu-sed
lrwxr-xr-x@ 1 mao  admin  21 Oct  5 22:58 /opt/homebrew/opt/gnu-sed@ -> ../Cellar/gnu-sed/4.9

❯ ls -al /opt/homebrew/opt/gsed
lrwxr-xr-x@ 1 mao  admin  21 Oct  5 22:58 /opt/homebrew/opt/gsed@ -> ../Cellar/gnu-sed/4.9

よって、以下のようにして GNU パッケージの PATH を一括で通すと良い、とな。


if type brew &>/dev/null; then
  HOMEBREW_PREFIX=$(brew --prefix)

  # gnubin; gnuman
  for d in ${HOMEBREW_PREFIX}/opt/*/libexec/gnubin; do export PATH=$d:$PATH; done

  # I actually like that man grep gives the BSD grep man page
  #for d in ${HOMEBREW_PREFIX}/opt/*/libexec/gnuman; do export MANPATH=$d:$MANPATH; done
fi

👉 macos - Homebrew: Easy way to add 'gnubin' to path for multiple packages? - Ask Different hatena-bookmark

他パッケージのインストーラーがこけたりするときないのか、と思ったので眺めておきました。