あるコマンドがどこから生まれたものなのか知りたいときはwhichコマンドが便利。
例えばカレントディレクトリを表示するコマンドであるpwdの実態を知りたいとき、
aaa@desktop:~$ pwd
/home/aaawhich [コマンド名]でどこにあるかを教えてくれる。
aaa@desktop:~$ which pwd
/usr/bin/pwdしかし、どこかで関数として定義されたmkaというコマンドに対して、whichは何も表示してくれない場面があった。
aaa@desktop:~/ubp-5.1$ mka -v
GNU Make 4.2.1
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
#### make completed successfully ####
aaa@desktop:~$ which mka
aaa@desktop:~$ そんなときはtypeを使う。
type [コマンド名]を打ち込むと、mkaがbash実行中に定義された関数であることが判明した。
aaa@desktop:~$ type mka
mka is a function
mka ()
{
local T=$(gettop);
if [ "$T" ]; then
case `uname -s` in
Darwin)
make -C $T -j `sysctl hw.ncpu|cut -d" " -f2` "$@"
;;
*)
mk_timer schedtool -B -n 1 -e ionice -n 1 make -C $T -j$(cat /proc/cpuinfo | grep "^processor" | wc -l) "$@"
;;
esac;
else
echo "Couldn't locate the top of the tree. Try setting TOP.";
fi
}
aaa@desktop:~$ type pwd
pwd is a shell builtin同じようにpwdにたいしてtypeで検索すると、shellのビルトインコマンドであることを教えてくれる。