Macでのwhereisコマンドとwhichコマンドの違い
whereis
もwhich
も、どちらもコマンドのパスを調べるコマンドです。
MacPortsで入れたコマンドに対してwhereis
しても、なぜか反応がなく、不思議だったので調べてみました。
結論
whereis
は/usr/bin:/bin:/usr/sbin:/sbin
にあるコマンドのみ調べられる。
which
は環境変数のPATHにあるコマンドから調べられる。
通常はwhich
を主に使ったほうが良いみたい。
whereisコマンド
$ man whereis WHEREIS(1) BSD General Commands Manual WHEREIS(1) NAME whereis -- locate programs SYNOPSIS whereis [program ...] DESCRIPTION The whereis utility checks the standard binary directories for the specified 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. SEE ALSO sysctl(8) COMPATIBILITY The historic flags and arguments for the whereis utility are no longer available in this version. HISTORY The whereis command appeared in 3.0BSD. 3rd Berkeley Distribution April 27, 1995 3rd Berkeley Distribution
The path searched is the string returned by the sysctl(8) utility for the ``user.cs_path'' string.
whereis
コマンドのmanページを見ると、こんな一文があります。
$ sysctl user.cs_path user.cs_path = /usr/bin:/bin:/usr/sbin:/sbin
実際にsysctl
を使ってみると、こんなパスが出てきます。
どうやら、whereis
コマンドは'checks the standard binary directories'という事のようです。
名称
http://www.jp.freebsd.org/cgi/mroff.cgi?subdir=man&lc=1&cmd=&man=sysctl&dir=jpman-5.4.0%2Fman§=0
sysctl - カーネル状態の取得や設定
ちなみに、sysctl
コマンドはこういうものらしい。
whichコマンド
$ man which WHICH(1) BSD General Commands Manual WHICH(1) NAME which -- locate a program file in the user's path SYNOPSIS which [-as] program ... 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 any of the executables are found, or 1 if none are found. Some shells may provide a builtin which command which is similar or identical to this utility. Consult the builtin(1) manual page. SEE ALSO builtin(1), csh(1), find(1), locate(1), whereis(1) HISTORY The which command first appeared in FreeBSD 2.1. AUTHORS The which utility was originally written in Perl and was contributed by Wolfram Schneider <wosch@FreeBSD.org>. The current version of which was rewritten in C by Daniel Papasian <dpapasia@andrew.cmu.edu>. BSD June 21, 2002 BSD
locate a program file in the user's path
この一文から、PATHを見てコマンドのパスを調べてくれることが伺えます。
実際にMacPortsで入れたコマンドのパスも調べられました。