3 回答

TA貢獻1813條經(jīng)驗 獲得超2個贊
回答
command -v <the_command>
bash
hash <the_command> # For regular commands. Or...type <the_command> # To check built-ins and keywords
解釋
which
hash
, type
command
許多操作系統(tǒng)都有一個 which
那,那個 甚至不設置退出狀態(tài)
,意思是 if which foo
甚至不會在那里工作 總
報告 foo
存在,即使它不存在(請注意,一些POSIX shell似乎是這樣做的。) hash
)。 許多操作系統(tǒng) which
做一些定制和邪惡的事情,比如改變輸出,甚至連接到包管理器。
which
$ command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }$ type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }$ hash foo 2>/dev/null || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
2>&-
2>/dev/null
2>&-
/bin/sh
type
hash
hash
type
command
bash
type
hash
type
-P
PATH
hash
gdate
date
:
gnudate() { if hash gdate 2>/dev/null; then gdate "$@" else date "$@" fi}

TA貢獻1817條經(jīng)驗 獲得超14個贊
$PATH
和
[ -x "$(command -v foo)" ]
if ! [ -x "$(command -v git)" ]; then echo 'Error: git is not installed.' >&2 exit 1fi
$PATH
.
$PATH
添加回答
舉報