轉自:http://codingstandards.iteye.com/blog/831504 html
在腳本中type可用於檢查命令或函數是否存在,存在返回0,表示成功;不存在返回正值,表示不成功。mysql
$ type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed. Aborting."; exit 1; }
type命令用來顯示指定命令的類型。一個命令的類型能夠是以下之一nginx
它是Linux系統的一種自省機制,知道了是那種類型,咱們就能夠針對性的獲取幫助。好比內建命令能夠用help命令來獲取幫助,外部命令用man或者info來獲取幫助。sql
type命令的基本使用方式就是直接跟上命令名字。shell
type -a能夠顯示全部可能的類型,好比有些命令如pwd是shell內建命令,也能夠是外部命令。編程
type -p只返回外部命令的信息,至關於which命令。bash
type -f只返回shell函數的信息。函數
type -t 只返回指定類型的信息。ui
[root@new55 ~]# type -a type
type is a shell builtin
[root@new55 ~]# help type
type: type [-afptP] name [name ...]
For each NAME, indicate how it would be interpreted if used as a
command name.
If the -t option is used, `type' outputs 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 unfound, respectively.
If the -p flag is used, `type' either returns the name of the disk
file that would be executed, or nothing if `type -t NAME' would not
return `file'.
If the -a flag is used, `type' displays all of the places that contain
an executable named `file'. This includes aliases, builtins, and
functions, if and only if the -p flag is not also used.
The -f flag suppresses shell function lookup.
The -P flag forces 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.
typeset: typeset [-afFirtx] [-p] name[=value] ...
Obsolete. See `declare'.
[root@new55 ~]#spa
[root@new55 ~]# type -a cd
cd is a shell builtin
[root@new55 ~]# type -a pwd
pwd is a shell builtin
pwd is /bin/pwd
[root@new55 ~]# type -a time
time is a shell keyword
time is /usr/bin/time
[root@new55 ~]# type -a date
date is /bin/date
[root@new55 ~]# type -a which
which is aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
which is /usr/bin/which
[root@new55 ~]# type -a whereis
whereis is /usr/bin/whereis
[root@new55 ~]# type -a whatis
whatis is /usr/bin/whatis
[root@new55 ~]# type -a function
function is a shell keyword
[root@new55 ~]# type -a ls
ls is aliased to `ls --color=tty'
ls is /bin/ls
[root@new55 ~]# type -a ll
ll is aliased to `ls -l --color=tty'
[root@new55 ~]# type -a echo
echo is a shell builtin
echo is /bin/echo
[root@new55 ~]# type -a bulitin
-bash: type: bulitin: not found
[root@new55 ~]# type -a builtin
builtin is a shell builtin
[root@new55 ~]# type -a keyword
-bash: type: keyword: not found
[root@new55 ~]# type -a command
command is a shell builtin
[root@new55 ~]# type -a alias
alias is a shell builtin
[root@new55 ~]# type -a grep
grep is /bin/grep
通常狀況下,type命令被用於判斷另一個命令是不是內置命令,可是它實際上有更多的用法。
1.判斷一個名字當前是不是alias、keyword、function、builtin、file或者什麼都不是:
type ls 的輸出是 ls 是 `ls --color=auto' 的別名
type if 的輸出是 if 是 shell 關鍵字
type type 的輸出是 type 是 shell 內嵌
type frydsh 的輸出是 bash: type: frydsh: 未找到
2.判斷一個名字當前是不是alias、keyword、function、builtin、file或者什麼都不是的另外一種方法(適用於腳本編程):
type -t ls 的輸出是 alias
type -t if 的輸出是 keyword
type -t type 的輸出是 builtin
type -t gedit 的輸出是 file
type -t frydsh 沒有輸出
3.顯示一個名字的全部可能:
type -a kill 的輸出是 kill 是 shell 內嵌 和 kill 是 /bin/kill
type -at kill 的輸出是 builtin 和 file
4.查看一個命令的執行路徑(若是它是外部命令的話):
type -p gedit 的輸出是 /usr/bin/gedit
type -p kill 沒有輸出(由於kill是內置命令)
5.強制搜索外部命令:
type -P kill 的輸出是 /bin/kill
原文連接:https://www.cnblogs.com/jxhd1/p/6699177.html
[root@localhost ~]# type ls
ls is aliased to `ls --color=tty'
[root@localhost ~]# type cd
cd is a shell builtin
[root@localhost ~]# type date
date is /bin/date
[root@localhost ~]# type mysql
mysql is /usr/bin/mysql
[root@localhost ~]# type nginx
-bash: type: nginx: not found
[root@localhost ~]# type if
if is a shell keyword
[root@localhost ~]# type which
which is aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# type -a cd
cd is a shell builtin
[root@localhost ~]# type -a grepgrep is /bin/grep