linux?type命令用法实战教程

 更新时间:2023年05月11日 11:04:15   作者:yuxi_o  
type命令用来显示指定命令的类型,它是Linux系统的一种自省机制,知道了是那种类型,我们就可以针对性的获取帮助,这篇文章主要介绍了linux?type命令用法实战教程,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

在脚本中type可用于检查命令或函数是否存在,存在返回0,表示成功;不存在返回正值,表示不成功。

$ type foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }

用途说明

type命令用来显示指定命令的类型。一个命令的类型可以是如下之一

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

它是Linux系统的一种自省机制,知道了是那种类型,我们就可以针对性的获取帮助。比如内建命令可以用help命令来获取帮助,外部命令用man或者info来获取帮助。

常用参数

type命令的基本使用方式就是直接跟上命令名字。

type -a可以显示所有可能的类型,比如有些命令如pwd是shell内建命令,也可以是外部命令。

type -p只返回外部命令的信息,相当于which命令。

type -f只返回shell函数的信息。

type -t 只返回指定类型的信息。

使用示例

示例一 type自己是什么类型的命令

[root@new55 ~]#?type -a type?
type is a shell builtin
[root@new55 ~]#?help type?
type: type [-afptP] name [name ...]
??? For each NAME, indicate how it would be interpreted if used as a
??? command name.
??? If the -t option is used, `type' outputs 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 unfound, respectively.
??? If the -p flag is used, `type' either returns the name of the disk
??? file that would be executed, or nothing if `type -t NAME' would not
??? return `file'.
??? If the -a flag is used, `type' displays all of the places that contain
??? an executable named `file'.? This includes aliases, builtins, and
??? functions, if and only if the -p flag is not also used.
??? The -f flag suppresses shell function lookup.
??? The -P flag forces 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.
typeset: typeset [-afFirtx] [-p] name[=value] ...
??? Obsolete.? See `declare'.
[root@new55 ~]#

示例二 常见命令的类型

[root@new55 ~]#?type -a cd?
cd is a shell builtin
[root@new55 ~]#?type -a pwd?
pwd is a shell builtin
pwd is /bin/pwd
[root@new55 ~]#?type -a time?
time is a shell keyword
time is /usr/bin/time
[root@new55 ~]#?type -a date?
date is /bin/date
[root@new55 ~]#?type -a which?
which is aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
which is /usr/bin/which
[root@new55 ~]#?type -a whereis?
whereis is /usr/bin/whereis
[root@new55 ~]#?type -a whatis?
whatis is /usr/bin/whatis
[root@new55 ~]#?type -a function?
function is a shell keyword
[root@new55 ~]#?type -a ls?
ls is aliased to `ls --color=tty'
ls is /bin/ls
[root@new55 ~]#?type -a ll?
ll is aliased to `ls -l --color=tty'
[root@new55 ~]#?type -a echo?
echo is a shell builtin
echo is /bin/echo
[root@new55 ~]#?type -a bulitin?
-bash: type: bulitin: not found
[root@new55 ~]#?type -a builtin?
builtin is a shell builtin
[root@new55 ~]#?type -a keyword?
-bash: type: keyword: not found
[root@new55 ~]#?type -a command?
command is a shell builtin
[root@new55 ~]#?type -a alias?
alias is a shell builtin
[root@new55 ~]#?type -a grep?
grep is /bin/grep

到此这篇关于linux type命令用法实战教程的文章就介绍到这了,更多相关linux type命令内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

  • linux Nginx 日志脚本

    linux Nginx 日志脚本

    这篇文章主要介绍了nginx日志切割脚本、nginx日志分析脚本等,需要的朋友可以参考下
    2013-11-11
  • Shell如何遍历包含空格的文本详解

    Shell如何遍历包含空格的文本详解

    这篇文章主要给大家介绍了关于Shell如何遍历包含空格的文本的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • linux下wc统计文件的个数、行数、字数、字节数等信息方法

    linux下wc统计文件的个数、行数、字数、字节数等信息方法

    下面小编就为大家带来一篇linux下wc统计文件的个数、行数、字数、字节数等信息方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • shell批量curl接口脚本的简单实现方法

    shell批量curl接口脚本的简单实现方法

    这篇文章主要跟大家介绍了关于shell批量curl接口脚本的简单实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用shell具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-08-08
  • Linux Shell脚本编程的注意事项

    Linux Shell脚本编程的注意事项

    这篇文章主要介绍了Linux下Shell脚本编程的一些注意事项,如编程风格、命名风格等,需要的朋友可以参考下
    2014-03-03
  • 浅谈shell的一些循环格式

    浅谈shell的一些循环格式

    这篇文章主要介绍了浅谈shell的一些循环格式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • 简单的远程FTP定时备份Shell脚本分享

    简单的远程FTP定时备份Shell脚本分享

    这篇文章主要介绍了简单的远程FTP定时备份Shell脚本分享,,需要的朋友可以参考下
    2014-07-07
  • Linux Shell脚本系列教程(五):数学运算

    Linux Shell脚本系列教程(五):数学运算

    这篇文章主要介绍了Linux Shell脚本系列教程(五):数学运算,本文讲解了使用let、(())和[]进行算术运算、使用expr进行算术运算、使用bc进行算术运算三种方法,需要的朋友可以参考下
    2015-06-06
  • 使用shell脚本来给mysql加索引的方法

    使用shell脚本来给mysql加索引的方法

    今天小编就为大家分享一篇关于使用shell脚本来给mysql加索引的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • Shell日志分析常用命令和例子

    Shell日志分析常用命令和例子

    这篇文章主要介绍了Shell日志分析常用命令和例子,本文重点在一样实现日志分析命令语句例子上,本文给出了10条常用的分析实例,需要的朋友可以参考下
    2014-12-12

最新评论

?


http://www.vxiaotou.com