?目錄
- 0?引言
- 1 type?命令的功能和格式
-
- 1.1?type命令的功能
- 1.2 type?命令的格式
- 2 type命令用法實(shí)例
-
- 2.1用type命令查看shell內(nèi)置命令(以echo命令為例)
- 2.2?用type命令查看別名(以ls命令為例)
- 2.3 用type命令同時(shí)查看shell內(nèi)置命令和別名(以echo和ls命令為例)
- 2.4?用type命令查看外部命令(以tty命令為例)
- 2.4?用type命令查看內(nèi)部命令、別名和外部命令(以echo、ls和tty命令為例)
- 2.5 用type 命令查看函數(shù)
- 2.6 如果我們用內(nèi)置命令或別名作為自
0?引言
在DOS中,type命令的功能是查看文件內(nèi)容。
而在Linux中,type命令的功能與DOS中的大相徑庭。
1 type?命令的功能和格式
我們可以使用 help type?命令查看 bash?中?關(guān)于type命令的幫助信息,其中包括了命令的功能?和格式。
purpleEndurer ?@ bash ~ $ help type
type: type [-afptP] name [name ...]
? ? Display information about command type.
? ??
? ? For each NAME, indicate how it would be interpreted if used as a
? ? command name.
? ??
? ? Options:
? ? ? -a ? ? ? ?display all locations containing an executable named NAME;
? ? ? ? includes aliases, builtins, and functions, if and only if
? ? ? ? the `-p' option is not also used
? ? ? -f ? ? ? ?suppress shell function lookup
? ? ? -P ? ? ? ?force a PATH search for each NAME, even if it is an alias,
? ? ? ? builtin, or function, and returns the name of the disk file
? ? ? ? that would be executed
? ? ? -p ? ? ? ?returns either the name of the disk file that would be executed,
? ? ? ? or nothing if `type -t NAME' would not return `file'.
? ? ? -t ? ? ? ?output a single word which is one of `alias', `keyword',
? ? ? ? `function', `builtin', `file' or `', if NAME is an alias, shell
? ? ? ? reserved word, shell function, shell builtin, disk file, or not
? ? ? ? found, respectively
? ??
? ? Arguments:
? ? ? NAME ? ? ?Command name to be interpreted.
? ??
? ? Exit Status:
? ? Returns success if all of the NAMEs are found; fails if any are not found.
typeset: typeset [-aAfFgilrtux] [-p] name[=value] ...
? ? Set variable values and attributes.
? ??
? ? Obsolete. ?See `help declare'.
purpleEndurer ?@ bash ~ $?
?1.1?type命令的功能
type命令?可以顯示指定命令的信息,判斷給出的指令是內(nèi)部命令、外部命令(文件)、別名、函數(shù)、保留字 或者 不存在(找不到)。
1.2 type?命令的格式
type [-afptP]?命令1 [命令2?...]
選項(xiàng)
選項(xiàng) | 說(shuō)明 | 備注 |
---|---|---|
-a | 顯示包含指定命令的可執(zhí)行文件的所有位置; 當(dāng)且僅當(dāng)未使用“-p”選項(xiàng)時(shí),包括別名、內(nèi)置函數(shù)和函數(shù) |
all |
-f | 禁止 查找 shell 函數(shù) | function |
-p | 如果給出的命令為外部指令,則顯示其絕對(duì)路徑 | path |
-P | 強(qiáng)制對(duì)給合的每個(gè)命令進(jìn)行 PATH 搜索,即使它是別名,內(nèi)置命令,或函數(shù),并返回將被執(zhí)行的磁盤文件的名稱? ? ? ?? | |
-t | 當(dāng)給定的命令為別名, shell保留字、shell 函數(shù)、shell 內(nèi)置命令、外部命令(磁盤文)件或?未找到時(shí),分別輸出“alias”, “keyword”, “function”, “builtin”, “file”?或?空。 | type |
2 type命令用法實(shí)例
2.1用type命令查看shell內(nèi)置命令(以echo命令為例)
purpleEndurer ?@ bash ~ $ type # 不接任何選項(xiàng)和參數(shù),無(wú)顯示
purpleEndurer ?@ bash ~ $ type echo # 接命令,顯示命令類型
echo is a shell builtin
purpleEndurer ?@ bash ~ $ type -t echo # 對(duì)內(nèi)部命令使用 -t 參數(shù),會(huì)顯示
# builtin,表示其為內(nèi)部命令
builtin
purpleEndurer ?@ bash ~ $ type -p echo # 對(duì)內(nèi)部命令使用 -p 參數(shù),無(wú)顯示
purpleEndurer ?@ bash ~ $ type -a echo # 使用 -a 參數(shù),會(huì)將PATH變量中
# 包含echo的命令顯示出來(lái)
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo
purpleEndurer @ bash ~ $ echo $PATH # 查看PATH變量的值
/home/csdn/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
purpleEndurer @ bash ~ $
?
2.2?用type命令查看別名(以ls命令為例)
purpleEndurer ?@ bash ~ $ type ls
ls is aliased to `ls --color=auto'
purpleEndurer ?@ bash ~ $ type -t ls
alias
purpleEndurer ?@ bash ~ $ type -p ls
purpleEndurer ?@ bash ~ $ type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
purpleEndurer ?@ bash ~ $?
?如果我們想執(zhí)行真正的那個(gè)命令而非別名,除了用
Linux shell編程學(xué)習(xí)筆記31:alias 和 unalias 操作 命令別名https://blog.csdn.net/Purpleendurer/article/details/134642886?spm=1001.2014.3001.5501
中的介紹的方法,還可以用type命令來(lái)判斷。
2.3 用type命令同時(shí)查看shell內(nèi)置命令和別名(以echo和ls命令為例)
purpleEndurer ?@ bash ~ $ type echo ls
echo is a shell builtin
ls is aliased to `ls --color=auto'purpleEndurer ?@ bash ~ $ type -t echo ls
builtin
alias
purpleEndurer ?@ bash ~ $ type -p echo ls
purpleEndurer ?@ bash ~ $ type -a echo ls
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/lspurpleEndurer ?@ bash ~ $
?
2.4?用type命令查看外部命令(以tty命令為例)
purpleEndurer ?@ bash ~ $ type tty
tty is /usr/bin/tty
purpleEndurer ?@ bash ~ $ type -p tty
/usr/bin/tty
purpleEndurer ?@ bash ~ $ type -P tty
/usr/bin/tty
purpleEndurer ?@ bash ~ $ type -t tty
file
purpleEndurer ?@ bash ~ $ type -a tty
tty is /usr/bin/tty
tty is /bin/tty
purpleEndurer ?@ bash ~ $ type -ap tty
/usr/bin/tty
/bin/tty
purpleEndurer ?@ bash ~ $ type -apt tty
file
file
purpleEndurer ?@ bash ~ $?
2.4?用type命令查看內(nèi)部命令、別名和外部命令(以echo、ls和tty命令為例)
purpleEndurer ?@ bash ~ $ type echo ls tty
echo is a shell builtin
ls is aliased to `ls --color=auto'
tty is /usr/bin/tty
purpleEndurer ?@ bash ~ $ type -apt echo ls tty
builtin
file
file
alias
file
file
file
file
purpleEndurer ?@ bash ~ $ type -a echo ls tty
echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
ls is /bin/ls
tty is /usr/bin/tty
tty is /bin/tty
purpleEndurer ?@ bash ~ $ type -at echo ls tty
builtin
file
file
alias
file
file
file
file
purpleEndurer ?@ bash ~ $ type -t echo ls tty
builtin
alias
file
purpleEndurer ?@ bash ~ $ type -p echo ls tty
/usr/bin/tty
purpleEndurer ?@ bash ~ $?
2.5 用type 命令查看函數(shù)
我們先定義一個(gè)函數(shù):
function a()
{
echo hello;
}
然后用type命令來(lái)查看:
purpleEndurer @ bash ~ $ function a(){ echo hello; }
purpleEndurer @ bash ~ $ type a
a is a function
a ()?
{?
? ? echo hello
}
purpleEndurer @ bash ~ $ type -a a
a is a function
a ()?
{?
? ? echo hello
}
purpleEndurer @ bash ~ $ type -f a
bash: type: a: not found
purpleEndurer @ bash ~ $ type -t a
function
purpleEndurer @ bash ~ $ type -p a
purpleEndurer @ bash ~ $ type -P a
purpleEndurer @ bash ~ $??
可見(jiàn),-p和-P選項(xiàng)對(duì)函數(shù)沒(méi)有作用。
2.6 如果我們用內(nèi)置命令或別名作為自定義函數(shù)名,type命令會(huì)如何顯示?
我們先定義一個(gè)函數(shù):
function ls()
{
echo hello;
}
然后用type命令來(lái)查看:
purpleEndurer @ bash ~ $ function ls(){ echo hello; }
purpleEndurer @ bash ~ $ ls
hello
purpleEndurer @ bash ~ $ type ls
ls is aliased to `ls --color=auto'
purpleEndurer @ bash ~ $ type -a ls?
ls is aliased to `ls --color=auto'
ls is a function
ls ()?
{?
? ? echo hello
}
ls is /usr/bin/ls
ls is /bin/ls
purpleEndurer @ bash ~ $ type -t ls?
alias
purpleEndurer @ bash ~ $ type -p ls?
purpleEndurer @ bash ~ $?
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-768423.html
從上面的命令執(zhí)行情況來(lái)看:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-768423.html
- 就執(zhí)行優(yōu)先級(jí)而言,函數(shù)優(yōu)先于內(nèi)置命令。
- 不加任何選項(xiàng)的話,type命令 不對(duì)函數(shù)進(jìn)行處理。
- 使用 -a 選項(xiàng),type命令 才對(duì)函數(shù)進(jìn)行處理。
到了這里,關(guān)于Linux shell編程學(xué)習(xí)筆記33:type 命令的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!