顯示指定命令的類(lèi)型。
type [-afptP] name [name ...]
-a:在環(huán)境變量PATH中查找并顯示所有包含name的可執(zhí)行文件路徑;當(dāng)'-p'選項(xiàng)沒(méi)有同時(shí)給出時(shí),如果在別名、關(guān)鍵字,函數(shù),內(nèi)建的信息中存在name,則一并顯示。
-f:排除對(duì)shell函數(shù)的查找。
-p:如果name在執(zhí)行'type -t name'返回的不是'file',那么什么也不返回;否則會(huì)在環(huán)境變量PATH中查找并返回可執(zhí)行文件路徑。
-P:即使要查找的name是別名、內(nèi)建、函數(shù)中的一個(gè),仍然會(huì)在環(huán)境變量PATH中查找并返回可執(zhí)行文件路徑。
-t:根據(jù)name的類(lèi)型返回一個(gè)單詞(別名,關(guān)鍵字,函數(shù),內(nèi)建,文件),否則返回空值。
name:要查找的命令,可以為多個(gè)。
當(dāng)指定的命令可以找到時(shí)返回成功,如果有沒(méi)找到的返回失敗。
接下來(lái)要用到的例子假設(shè)'~/.bashrc'文件定義了以下的內(nèi)容:
alias ls='ls --color=auto'
mybash(){ vim ~/.bashrc; }
而且執(zhí)行環(huán)境里沒(méi)有使用enable禁用內(nèi)建命令。
type -a mybash
# 輸出
mybash is a function
mybash ()
{
vim ~/.bashrc
}
type -a -f mybash
# 輸出(因?yàn)榕懦撕瘮?shù),所以報(bào)錯(cuò))
bash: type: mybash: not found
type -a -p mybash
# 輸出為空(因?yàn)榕懦撕瘮?shù),所以什么也不返回)
type -a ls
# 輸出
ls is aliased to `ls --color=suto'
ls is /usr/bin/ls
ls is /bin/ls
type -a -p ls
# 輸出
/usr/bin/ls
/bin/ls
# '-f'不會(huì)影響'-P'的范圍,'-f'不建議和'-p'使用。
# 注意:printf同時(shí)是內(nèi)建命令以及可執(zhí)行文件(GNU coreutils),優(yōu)先作為內(nèi)建處理。
type -p printf
# 輸出為空
type -P printf
# 輸出
/usr/bin/printf
/bin/printf
# 如果有多個(gè)類(lèi)型,那么輸出優(yōu)先級(jí)最高的類(lèi)型。
type -t ls
# 輸出
alias
type -t for
# 輸出(bash關(guān)鍵字)
keyword
type -t mybash
# 輸出
function
type -t -f mybash
# 輸出空值
type -t printf
# 輸出(bash內(nèi)建優(yōu)先級(jí)高)
builtin
type -t chmod
# 輸出
file
該命令是bash內(nèi)建命令,相關(guān)的幫助信息請(qǐng)查看help
命令。
命令優(yōu)先級(jí)問(wèn)題請(qǐng)查看builtin
命令。