赞
踩
目录
在DOS中,type命令的功能是查看文件内容。
而在Linux中,type命令的功能与DOS中的大相径庭。
我们可以使用 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 ~ $
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 |
- 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 ~ $
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 ~ $
如果我们想执行真正的那个命令而非别名,除了用
中的介绍的方法,还可以用type命令来判断。
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 ~ $
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 ~ $
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 ~ $
我们先定义一个函数:
- 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选项对函数没有作用。
我们先定义一个函数:
- 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 ~ $
从上面的命令执行情况来看:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。