Linux shell编程学习笔记33:type 命令

👁️ 4104 ❤️ 258
Linux shell编程学习笔记33:type 命令

目录

0 引言1 type 命令的功能和格式

1.1 type命令的功能1.2 type 命令的格式2 type命令用法实例

2.1用type命令查看shell内置命令(以echo命令为例)2.2 用type命令查看别名(以ls命令为例)2.3 用type命令同时查看shell内置命令和别名(以echo和ls命令为例)2.4 用type命令查看外部命令(以tty命令为例)2.4 用type命令查看内部命令、别名和外部命令(以echo、ls和tty命令为例)2.5 用type 命令查看函数2.6 如果我们用内置命令或别名作为自

0 引言

在DOS中,type命令的功能是查看文件内容。

而在Linux中,type命令的功能与DOS中的大相径庭。

1 type 命令的功能和格式

我们可以使用 help type 命令查看 bash 中 关于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命令 可以显示指定命令的信息,判断给出的指令是内部命令、外部命令(文件)、别名、函数、保留字 或者 不存在(找不到)。

1.2 type 命令的格式

type [-afptP] 命令1 [命令2 ...]

选项

选项说明备注-a 显示包含指定命令的可执行文件的所有位置;

当且仅当未使用“-p”选项时,包括别名、内置函数和函数

all-f禁止 查找 shell 函数function-p如果给出的命令为外部指令,则显示其绝对路径path-P强制对给合的每个命令进行 PATH 搜索,即使它是别名,内置命令,或函数,并返回将被执行的磁盘文件的名称 -t当给定的命令为别名, shell保留字、shell 函数、shell 内置命令、外部命令(磁盘文)件或 未找到时,分别输出“alias”, “keyword”, “function”, “builtin”, “file” 或 空。type

2 type命令用法实例

2.1用type命令查看shell内置命令(以echo命令为例)

purpleEndurer @ bash ~ $ type # 不接任何选项和参数,无显示

purpleEndurer @ bash ~ $ type echo # 接命令,显示命令类型

echo is a shell builtin

purpleEndurer @ bash ~ $ type -t echo # 对内部命令使用 -t 参数,会显示

# builtin,表示其为内部命令

builtin

purpleEndurer @ bash ~ $ type -p echo # 对内部命令使用 -p 参数,无显示

purpleEndurer @ bash ~ $ type -a echo # 使用 -a 参数,会将PATH变量中

# 包含echo的命令显示出来

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 ~ $

如果我们想执行真正的那个命令而非别名,除了用

Linux shell编程学习笔记31:alias 和 unalias 操作 命令别名https://blog.csdn.net/Purpleendurer/article/details/134642886?spm=1001.2014.3001.5501

中的介绍的方法,还可以用type命令来判断。

2.3 用type命令同时查看shell内置命令和别名(以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/ls

purpleEndurer @ 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命令查看内部命令、别名和外部命令(以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 命令查看函数

我们先定义一个函数:

function a()

{

echo hello;

}

然后用type命令来查看:

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 ~ $

可见,-p和-P选项对函数没有作用。

2.6 如果我们用内置命令或别名作为自定义函数名,type命令会如何显示?

我们先定义一个函数:

function ls()

{

echo hello;

}

然后用type命令来查看:

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 ~ $

从上面的命令执行情况来看:

就执行优先级而言,函数优先于内置命令。不加任何选项的话,type命令 不对函数进行处理。使用 -a 选项,type命令 才对函数进行处理。

← 汉朝地位边缘的并州,为何在魏晋南北朝成了风云之地? 英雄联盟战争学院人物一览表 →