rexian

咨询电话:023-6276-4481

热门文章

联系方式

电 话:023-6276-4481

邮箱:broiling@qq.com

地址:重庆市南岸区亚太商谷6幢25-2

当前位置:网站首页 > 技术文章 > 给初学者的 type 命令教程

给初学者的 type 命令教程

编辑:pansiqin 发表时间:2018-04-09 13:40:15
pansiqin

type 命令用于查找 Linux 命令的信息。顾名思义,你可以使用 type 命令轻松找出给定的命令是否是别名、shell 内置命令、文件、函数或关键字。另外,你也可以找到命令的实际路径。为什么有人需要找到命令类型?例如,如果你经常在共享的计算机上工作,则某些人可能会故意或意外地为特定的 Linux 命令创建别名来执行不需要的操作,例如 alias ls = rm -rf /。因此,在发生更糟糕的事情之前检查它们总是一个好主意。这是 type 命令有用的地方。

让我给你看一些例子。

不带任何选项运行 type 命令。

$ type ls ls is aliased to `ls --color=auto'

12$ type lsls is aliased to `ls --color=auto'

$ type ls

ls is aliased to `ls --color=auto'

正如你在上面的输出中看到的那样,ls 命令已被别名为 ls -color-auto。但是,它是无害的。但只要想想 ls 如果被别名为了其他一些危险的命令。你不想那样,是吗?

你可以使用 -t 选项仅找出 Linux 命令的类型。例如:

$ type -t ls alias $ type -t mkdir file $ type -t pwd builtin $ type -t if keyword $ type -t rvm function

12345678910$ type -t lsalias$ type -t mkdirfile$ type -t pwdbuiltin$ type -t ifkeyword$ type -t rvmfunction

10

$ type -t ls

alias

$ type -t mkdir

file

$ type -t pwd

builtin

$ type -t if

keyword

$ type -t rvm

function

该命令仅显示命令的类型,例如别名。它不显示被别名的内容。如果该命令找不到,你将在终端中看不到任何内容。

type 命令的另一个有用的地方是我们可以很容易地找出给定 Linux 命令的绝对路径。为此,请使用 -p 选项,如下所示。

$ type -p cal /usr/bin/cal

12$ type -p cal/usr/bin/cal

$ type -p cal

/usr/bin/cal

这与 which ls 命令类似。如果给定的命令是别名,则不会打印任何内容。

要显示命令的所有信息,请使用 -a 选项。

$ type -a ls ls is aliased to `ls --color=auto' ls is /usr/bin/ls ls is /bin/ls

1234$ type -a lsls is aliased to `ls --color=auto'ls is /usr/bin/lsls is /bin/ls

$ type -a ls

ls is aliased to `ls --color=auto'

ls is /usr/bin/ls

ls is /bin/ls