|
1 | 1 | type |
2 | 2 | === |
3 | 3 |
|
4 | | -显示指定命令的类型 |
| 4 | +显示指定命令的类型。 |
5 | 5 |
|
6 | | -## 补充说明 |
| 6 | +## 概要 |
7 | 7 |
|
8 | | -**type命令** 用来显示指定命令的类型,判断给出的指令是内部指令还是外部指令。 |
| 8 | +```shell |
| 9 | + type [-afptP] name [name ...] |
| 10 | + ``` |
9 | 11 |
|
10 | | -命令类型: |
| 12 | +## 主要用途 |
11 | 13 |
|
12 | | -* alias:别名。 |
13 | | -* keyword:关键字,Shell保留字。 |
14 | | -* function:函数,Shell函数。 |
15 | | -* builtin:内建命令,Shell内建命令。 |
16 | | -* file:文件,磁盘文件,外部命令。 |
17 | | -* unfound:没有找到。 |
| 14 | +- 显示要查找的命令的信息。 |
| 15 | +- 控制查找范围和行为。 |
| 16 | +- 显示要查找的命令优先级最高的类型。 |
18 | 17 |
|
19 | | -### 语法 |
| 18 | +## 选项 |
20 | 19 |
|
21 | 20 | ```shell |
22 | | -type(选项)(参数) |
| 21 | +-a:在环境变量PATH中查找并显示所有包含name的可执行文件路径;当'-p'选项没有同时给出时,如果在别名、关键字,函数,内建的信息中存在name,则一并显示。 |
| 22 | +-f:排除对shell函数的查找。 |
| 23 | +-p:如果name在执行'type -t name'返回的不是'file',那么什么也不返回;否则会在环境变量PATH中查找并返回可执行文件路径。 |
| 24 | +-P:即使要查找的name是别名、内建、函数中的一个,仍然会在环境变量PATH中查找并返回可执行文件路径。 |
| 25 | +-t:根据name的类型返回一个单词(别名,关键字,函数,内建,文件),否则返回空值。 |
23 | 26 | ``` |
24 | 27 |
|
25 | | -### 选项 |
| 28 | +## 参数 |
| 29 | + |
| 30 | +name:要查找的命令,可以为多个。 |
| 31 | + |
| 32 | +## 返回值 |
| 33 | + |
| 34 | +当指定的命令可以找到时返回成功,如果有没找到的返回失败。 |
| 35 | + |
| 36 | +## 例子 |
26 | 37 |
|
27 | 38 | ```shell |
28 | | --t:输出“file”、“alias”或者“builtin”,分别表示给定的指令为“外部指令”、“命令别名”或者“内部指令”; |
29 | | --p:如果给出的指令为外部指令,则显示其绝对路径; |
30 | | --a:在环境变量“PATH”指定的路径中,显示给定指令的信息,包括命令别名。 |
31 | | -``` |
| 39 | +接下来要用到的例子假设'~/.bashrc'文件定义了以下的内容: |
32 | 40 |
|
33 | | -### 参数 |
| 41 | +alias ls='ls --color=auto' |
| 42 | +mybash(){ vim ~/.bashrc; } |
34 | 43 |
|
35 | | -指令:要显示类型的指令。 |
| 44 | +而且执行环境里没有使用enable禁用内建命令。 |
| 45 | +``` |
36 | 46 |
|
37 | | -### 实例 |
| 47 | +```shell |
| 48 | +type -a mybash |
| 49 | +# 输出 |
| 50 | +mybash is a function |
| 51 | +mybash () |
| 52 | +{ |
| 53 | + vim ~/.bashrc |
| 54 | +} |
| 55 | + |
| 56 | +type -a -f mybash |
| 57 | +# 输出(因为排除了函数,所以报错) |
| 58 | +bash: type: mybash: not found |
| 59 | + |
| 60 | +type -a -p mybash |
| 61 | +# 输出为空(因为排除了函数,所以什么也不返回) |
| 62 | + |
| 63 | +type -a ls |
| 64 | +# 输出 |
| 65 | +ls is aliased to `ls --color=suto' |
| 66 | +ls is /usr/bin/ls |
| 67 | +ls is /bin/ls |
| 68 | +
|
| 69 | +type -a -p ls |
| 70 | +# 输出 |
| 71 | +/usr/bin/ls |
| 72 | +/bin/ls |
| 73 | +``` |
38 | 74 |
|
39 | 75 | ```shell |
40 | | -[root@localhost ~]# type ls |
41 | | -ls is aliased to `ls --color=tty' |
| 76 | +# '-f'不会影响'-P'的范围,'-f'不建议和'-p'使用。 |
| 77 | +# 注意:printf同时是内建命令以及可执行文件(GNU coreutils),优先作为内建处理。 |
| 78 | +
|
| 79 | +type -p printf |
| 80 | +# 输出为空 |
42 | 81 |
|
43 | | -[root@localhost ~]# type cd |
44 | | -cd is a shell builtin |
| 82 | +type -P printf |
| 83 | +# 输出 |
| 84 | +/usr/bin/printf |
| 85 | +/bin/printf |
| 86 | +``` |
45 | 87 |
|
46 | | -[root@localhost ~]# type date |
47 | | -date is /bin/date |
| 88 | +```shell |
| 89 | +# 如果有多个类型,那么输出优先级最高的类型。 |
48 | 90 |
|
49 | | -[root@localhost ~]# type mysql |
50 | | -mysql is /usr/bin/mysql |
| 91 | +type -t ls |
| 92 | +# 输出 |
| 93 | +alias |
51 | 94 |
|
52 | | -[root@localhost ~]# type nginx |
53 | | --bash: type: nginx: not found |
| 95 | +type -t for |
| 96 | +# 输出(bash关键字) |
| 97 | +keyword |
54 | 98 |
|
55 | | -[root@localhost ~]# type if |
56 | | -if is a shell keyword |
| 99 | +type -t mybash |
| 100 | +# 输出 |
| 101 | +function |
57 | 102 |
|
58 | | -[root@localhost ~]# type which |
59 | | -which is aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' |
| 103 | +type -t -f mybash |
| 104 | +# 输出空值 |
60 | 105 |
|
61 | | -[root@localhost ~]# type -a cd |
62 | | -cd is a shell builtin |
| 106 | +type -t printf |
| 107 | +# 输出(bash内建优先级高) |
| 108 | +builtin |
63 | 109 |
|
64 | | -[root@localhost ~]# type -a grep |
65 | | -grep is /bin/grep |
| 110 | +type -t chmod |
| 111 | +# 输出 |
| 112 | +file |
66 | 113 | ``` |
67 | 114 |
|
| 115 | +### 注意 |
| 116 | +
|
| 117 | +1. 该命令是bash内建命令,相关的帮助信息请查看`help`命令。 |
| 118 | +
|
| 119 | +2. 命令优先级问题请查看`builtin`命令。 |
| 120 | +
|
68 | 121 |
|
69 | | -<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ --> |
| 122 | +<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ --> |
0 commit comments