水島雄太のブログ

個人的かつ雑多なブログです。

コマンド本体の実行パスを出力するコマンド一覧とその機能の比較

はじめに

実行したいコマンドの実行パスを知りたいときに whichwhereis を違いをよく知らずに使っており、そのことをふとした瞬間で恥じてしまったため、違いをまとめます。

macOS 10.14.6 に最初から入っているコマンドを対象にしています。

コマンド本体の実行パスを出力するコマンド一覧

whereis

NAME
     whereis -- locate programs

...
DESCRIPTION
     The whereis utility checks the standard binary directories for the speci-
     fied programs, printing out the paths of any it finds.

     The path searched is the string returned by the sysctl(8) utility for the
     ``user.cs_path'' string.
  • sysctl user.cs_path で出力されるパスが検索対象になる
    • macOS のデフォルト設定だと /usr/bin:/bin:/usr/sbin:/sbin
    • PATH で設定されたパスは検索対象にならない

which

NAME
     which -- locate a program file in the user's path

...

DESCRIPTION
     The which utility takes a list of command names and searches the path for
     each executable file that would be run had these commands actually been
     invoked.

     The following options are available:

     -a      List all instances of executables found (instead of just the
             first one of each).

     -s      No output, just return 0 if all of the executables are found, or
             1 if some were not found.

     Some shells may provide a builtin which command which is similar or iden-
     tical to this utility.  Consult the builtin(1) manual page.
  • PATH で設定された優先順位で実行パスを検索する
  • 実行パスをリスト形式で出力する
  • -a で すべての実行パスを出力(このオプションがなければ最初の実行パスしか出力されない
  • -s で何も出力せず、終了ステータスのみ返す
  • builtin which と似ているか、あるいはどちらかのシノニムになっている
    • このあたりはおそらく環境依存
  • 元々はPerl
    • Wolfram Schneide 作
  • 現在のversion は Daniel Papasian がCで書き直した

where

DESCRIPTION
     Shell builtin commands are commands that can be executed within the run-
     ning shell's process.  Note that, in the case of csh(1) builtin commands,
     the command is executed in a subshell if it occurs as any component of a
     pipeline except the last.
  • コマンドオプションは見当たらない
    • -s などは使えるが違いがよくわからない
  • whichと違いデフォルトですべての実行パスを優先度順に表示する
  • シェルのビルトインコマンド
    • シェルを実行パス検索順に表示されるのでシェルから実行する場合はこれの出力が一番正確

type

DESCRIPTION
     Shell builtin commands are commands that can be executed within the run-
     ning shell's process.  Note that, in the case of csh(1) builtin commands,
     the command is executed in a subshell if it occurs as any component of a
     pipeline except the last.
  • whichのシノニム

参考文献