Linux命令完全指南

Linux type命令使用详解

Linux type命令用来显示指定命令的类型,判断给出的指令是内部指令还是外部指令。

命令类型:

  • alias:别名。
  • keyword:关键字,Shell保留字。
  • function:函数,Shell函数。
  • builtin:内建命令,Shell内建命令。
  • file:文件,磁盘文件,外部命令。
  • unfound:没有找到。

语法

1
type [-afptP] name [name ...]

选项

1
2
3
4
5
-a:在环境变量PATH中查找并显示所有包含name的可执行文件路径;当'-p'选项没有同时给出时,如果在别名、关键字,函数,内建的信息中存在name,则一并显示。
-f:排除对shell函数的查找。
-p:如果name在执行'type -t name'返回的不是'file',那么什么也不返回;否则会在环境变量PATH中查找并返回可执行文件路径。
-P:即使要查找的name是别名、内建、函数中的一个,仍然会在环境变量PATH中查找并返回可执行文件路径。
-t:根据name的类型返回一个单词(别名,关键字,函数,内建,文件),否则返回空值。

参数

name:要查找的命令,可以为多个。

主要用途

  • 显示要查找的命令的信息。
  • 控制查找范围和行为。
  • 显示要查找的命令优先级最高的类型

返回值

当指定的命令可以找到时返回成功,如果有没找到的返回失败。

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[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 grep
grep is /bin/grep
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
type -a mybash
# 输出
mybash is a function
mybash ()
{
vim ~/.bashrc
}
type -a -f mybash
# 输出(因为排除了函数,所以报错)
bash: type: mybash: not found
type -a -p mybash
# 输出为空(因为排除了函数,所以什么也不返回)
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