Shell
為什麼“which”命令會給出重複的結果?
which -a ruby
給我/usr/ruby /usr/ruby /usr/ruby
它給出了三遍相同的路徑。為什麼會這樣?
檢查你的路徑。最終出現重複並不難。例子:
»echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin: »which -a bash /bin/bash /usr/bin/bash
這是因為我的 /bin 是 /usr/bin 的符號連結。現在:
»export PATH=$PATH:/usr/bin »echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/bin »which -a bash /bin/bash /usr/bin/bash /usr/bin/bash
由於 /usr/bin 現在在我的 $PATH 中兩次,因此兩次
which -a
找到相同的 bash。
正如提示所說,並從手冊頁中引用,
"Which takes one or more arguments. For each of its arguments it prints to stdout the full path of the executables that would have been executed when this argument had been entered at the shell prompt. It does this by searching for an executable or script in the directories listed in the environment variable PATH using the same algorithm as bash(1)."
至於-a
選項,它列出了在 $PATH 中找到的該名稱的所有執行檔。