いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

path_helper ($PATHを設定するコマンド) (macOS, /etc/paths.d, /etc/paths, shell間をまたいだパス設定)

概要

ずっと謎だったことが解決して、忘れないうちにメモメモ。

(単純に私が知らなかっただけなのですが)

macOSで、.NET Core をインストールすると、dotnetコマンドが使えるようになります。

んで、

$ which dotnet

ってすると

~$ which dotnet
/usr/local/share/dotnet/dotnet

って出ます。

でも、自分の設定ファイル

  • $HOME/.bashrc
  • $HOME/.bash_profile
  • /etc/bashrc
  • /etc/profile

みても、どこにもそんなパス設定していないのです。でもパスが通ってる・・・。謎・・・・。

ってずっと思っていました。まあ使えるからいいやって思ってました。

んで、heroku絡みで Postgres.app ってのをインストールしようとして、ドキュメントみてると

sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

ってコマンドを打ち込んでねって書いてありました。

ん? /etc/paths.dって何これ?

ちょっと調べてみると、こいつが出てきた。

path_helper

path_helper さん

そういえば、こいつ /etc/profileで一番最初に処理されているやつです。

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

if [ "${BASH-no}" != "no" ]; then
    [ -r /etc/bashrc ] && . /etc/bashrc
fi

man で見てみる。

$ man path_helper 

説明のところに、件の/etc/paths.dが出てきた。

DESCRIPTION
     The path_helper utility reads the contents of the files in the directories /etc/paths.d and /etc/manpaths.d and appends
     their contents to the PATH and MANPATH environment variables respectively.  (The MANPATH environment variable will not
     be modified unless it is already set in the environment.)

     Files in these directories should contain one path element per line.

     Prior to reading these directories, default PATH and MANPATH values are obtained from the files /etc/paths and
     /etc/manpaths respectively.

以下のルールのようですね。

  • デフォルトのパス設定は、/etc/pathsファイルから読み取られる。

  • 残りは、/etc/paths.dディレクトリの下から読み取られる。

なるほど。。。てことで見てみる。

~$ file /etc/paths
/etc/paths: ASCII text
~$ cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

そのまんま記述されているんですね。次は、/etc/paths.d

~$ ls /etc/paths.d
dotnet        mono-commands
~$ file /etc/paths.d/dotnet 
/etc/paths.d/dotnet: ASCII text
~$ cat /etc/paths.d/dotnet 
/usr/local/share/dotnet

いた!!ここで、dotnet のパス設定されてたんですね。

てことで、上のPostgres.appのサイトに記載されている

sudo mkdir -p /etc/paths.d &&
echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

は、このアプリのパスを通すために、ここにファイルを出力しようとしていたのですね。納得。

なので、この方式でパス設定しておくと、shellを変更しても設定し直さなくていい。

知ると便利ですね。

ちなみに、zsh の方の /etc/zprofileにも同じようにpath_helperの設定がありました。


過去の記事については、以下のページからご参照下さい。

サンプルコードは、以下の場所で公開しています。