W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
顯示指定命令的類型。
type [-afptP] name [name ...]
-a:在環(huán)境變量PATH中查找并顯示所有包含name的可執(zhí)行文件路徑;當(dāng)'-p'選項(xiàng)沒有同時(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的類型返回一個(gè)單詞(別名,關(guān)鍵字,函數(shù),內(nèi)建,文件),否則返回空值。
name:要查找的命令,可以為多個(gè)。
當(dāng)指定的命令可以找到時(shí)返回成功,如果有沒找到的返回失敗。
接下來要用到的例子假設(shè)'~/.bashrc'文件定義了以下的內(nèi)容:
alias ls='ls --color=auto'
mybash(){ vim ~/.bashrc; }
而且執(zhí)行環(huán)境里沒有使用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è)類型,那么輸出優(yōu)先級(jí)最高的類型。
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
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: