首頁·終端機命令

which

行程與系統

用法

which [-a] <name>

在 PATH 裡尋找同名執行檔在哪;因為它只看 PATH,對別名、函式和 shell 內建命令會給出錯誤答案,老實的答案來自 `type -a` 或 `command -v`。

常用選項

選項含義
-aShow every match in PATH, not just the first one.
<name> <name>Several names at once; the exit status is nonzero if any is missing.
-sPrint nothing, just set the exit status (BSD/macOS).

範例

which -a python3

Spot a second python3 earlier in PATH than the one you expected.

command -v node || echo missing

The portable check to use in scripts.

看什麼在跑、什麼在吃機器。先弄清是什麼,再決定殺不殺。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. which 是做什麼的?

在 PATH 裡尋找同名執行檔在哪;因為它只看 PATH,對別名、函式和 shell 內建命令會給出錯誤答案,老實的答案來自 `type -a` 或 `command -v`。

Q. 怎麼寫?

which [-a] <name> —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 3 個,完整列表在 man which。這條命令屬於行程與系統。

相關命令

man 手冊: man which