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

 目录

  1. 0 引言
  2. 1 type 命令的功能和格式
    1. 1.1 type命令的功能
    2. 1.2 type 命令的格式
  3. 2 type命令用法实例
    1. 2.1用type命令查看shell内置命令(以echo命令为例)
    2. 2.2 用type命令查看别名(以ls命令为例)
    3. 2.3 用type命令同时查看shell内置命令和别名(以echo和ls命令为例)
    4. 2.4 用type命令查看外部命令(以tty命令为例)
    5. 2.4 用type命令查看内部命令、别名和外部命令(以echo、ls和tty命令为例)
    6. 2.5 用type 命令查看函数
    7. 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 操作 命令别名icon-default.png?t=N7T8https://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命令 才对函数进行处理。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/203593.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

抽象类和接口(超重点!!)

[本节目标] 1.抽象类 2.接口 3.Object类 1.抽象类 1.1 抽象类概念 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象&a…

LabVIEW发开发电状态监测系统

LabVIEW发开发电状态监测系统 对发电设备的持续监测对于确保可靠的电力供应至消费者极为重要。它不仅能够及时提醒操作员注意发电设备的潜在损坏,还能减少由于设备故障造成的停机时间。为了达到这一目标,开发了一款基于LabVIEW的软件,专门用…

C语言进阶之路之内存镜像与字符操作函数篇

目录 一、学习目标: 二、内存镜像 什么是进程 C进程内存布局 栈内存 静态数据 数据段(存储静态数据)与代码段 堆内存 三、字符操作函数 函数strstr 函数strlen strlen与sizeof的区别 函数strtok 函数strcat与strncat 函数strc…

远程调用后 List为什么会转换成LinkedHashMap

远程调用后 List&#xff1c;?&#xff1e;为什么会转换成LinkedHashMap 在 Java 中&#xff0c;当你通过远程调用&#xff08;如 REST API、RPC 等&#xff09;传递泛型集合&#xff08;如 List<?>&#xff09;时&#xff0c;由于 Java 的类型擦除和序列化的特性&…

echarts 提示框 自动轮播(解决getZr is not a function)

echarts-auto.js (function (global) {global.tools global.tools || {};/*** echarts tooltip 自动轮播* author liuyishi* param chart* param chartOption* param options* {* interval 轮播时间间隔&#xff0c;单位毫秒&#xff0c;默认为2000* loopSeries …

【单体架构事务失效解决方式之___代理对象加锁】

单体架构__用户限买 一个id一单的多线程事务失效问题解决 背景介绍&#xff1a;有一种情况&#xff0c;我们在使用Synchronized的时候出现失效情况。 经过排查&#xff0c;是因为使用了this.当前对象&#xff0c;他现在使用的是目标对象加锁失效&#xff0c;使用代理对象加锁就…

string类:`reserve()`,`resize()`详解

string类的重要接口说明(1)&#xff1a;reserve()&#xff0c;resize() 一、reserve() 和 resize() 注意不要把 reserve(储备) 和 reverse(逆向) 搞混了&#xff01; 1.1 reserve()&#xff1a;用于为字符串预留空间&#xff08;扩容&#xff09;&#xff0c;以便后续操作可以…

【MySQL数据类型】

目录&#xff1a; 前言数据类型分类整数类型tinyintbit 小数类型floatdecimal 字符串类型charvarchar日期和时间enum & set在集合中查找find_in_set 前言 剑指offer&#xff1a;一年又4天 数据类型分类 整数类型 tinyint 整数类型都分为有符号和无符号两种&#xff0c;默…

Linux重要基本命令

重要基本命令 1.ls命令 语法&#xff1a; ls [选项][目录或文件] 功能&#xff1a;对于目录&#xff0c;该命令列出该目录下的所有子目录与文件。对于文件&#xff0c;将列出文件名以及其他信息。 常见的选项包括&#xff1a; -l&#xff1a;以长格式显示文件信息&#xf…

git 使用记录

远程仓库为空初始化 初始化本地仓库 git init 在本地仓库书写代码&#xff08;这里可以编辑一个文本文件做测试&#xff0c;如hello.txt&#xff09; 5&#xff09;执行&#xff1a;git add 要让git管理的文件&#xff08;git add hello.txt&#xff09;>执行完此操作将我…

git提交报错 fatal: LF would be replaced by CRLF in package-lock.json

报错 fatal: LF would be replaced by CRLF in package-lock.json 原因 git 在windows下&#xff0c;默认是CRLF作为换行符&#xff0c; git add 提交时&#xff0c;会检查文本中是否有LF 换行符&#xff08;linux系统&#xff09;&#xff0c;如果有则会告警&#xff0c; 所…

手持机|三防智能手机_4寸/5寸/6寸安卓系统三防手机PDA手持终端方案

随着科技的不断发展&#xff0c;三防手持机作为一种多功能设备&#xff0c;正逐渐在各行业得到广泛应用。这款手持机采用高性能处理器&#xff0c;支持高精度北斗定位和工业本安防爆功能&#xff0c;并具备IP67级防水防尘性能和1.5米防跌落能力。因此&#xff0c;它在仓储管理、…

【Docker】从零开始:17.Dockerfile基本概念

【Docker】从零开始&#xff1a;17.Dockerfile 概述1.什么是Dockerfile2.Dockerfile构建三大步骤3.Docker执行Dockerfile流程 一张图理解Dockerfile常用保留指令~FROM~~MAINTAINER~~RUN~两种格式 ~EXPOSE~~WORKDIR~~USER~~ENV~~ADD~~COPY~两种格式 ~VOLUME~~CMD~两种格式注意 ~…

算能PCIe开发环境搭建-一些记录

开发环境与运行环境&#xff1a; 开发环境是指用于模型转换或验证以及程序编译等开发过程的环境&#xff1b;运行环境是指在具备Sophon设备的平台上实际使用设备进行算法应用部署的运行环境。 开发环境与运行环境可能是统一的&#xff08;如插有SC5加速卡的x86主机&#xff0c;…

Mac端 DevEco Preview 窗口无法展示,提示文件中的node.dir错误

语雀知识库地址&#xff1a;语雀HarmonyOS知识库 飞书知识库地址&#xff1a;飞书HarmonyOS知识库 DevEco版本&#xff1a;Build Version: 3.1.0.501, built on June 20, 2023 环境信息 问题描述 打开 Preview 标签窗口后&#xff0c;提示Preview failed。 Run窗口提示如下 F…

Stable Diffusion AI绘画系列【18】:东方巨龙,威武霸气

《博主简介》 小伙伴们好&#xff0c;我是阿旭。专注于人工智能、AIGC、python、计算机视觉相关分享研究。 ✌更多学习资源&#xff0c;可关注公-仲-hao:【阿旭算法与机器学习】&#xff0c;共同学习交流~ &#x1f44d;感谢小伙伴们点赞、关注&#xff01; 《------往期经典推…

PyTorch 基础篇(2):线性回归(Linear Regression)

# 包import torchimport torch.nn as nnimport numpy as npimport matplotlib.pyplot as plt # 超参数设置input_size 1output_size 1num_epochs 60learning_rate 0.001 # Toy dataset # 玩具资料&#xff1a;小数据集x_train np.array([[3.3], [4.4], [5.5], [6.71], [6.…

ssh免密远程登录主机并执行命令

命令&#xff1a;ssh -t -t root192.168.11.2 "cd /home/test/; ls -l"这条命令是使用 SSH 客户端连接到远程主机 192.168.11.2&#xff0c;并以 root 用户的身份登录。具体来说&#xff0c;命令的各部分作用如下&#xff1a; ssh: 开始 SSH 客户端。-t -t: 告诉 SS…

python使用flask框架实现http服务处理

1&#xff0c;python使用flask框架实现http接口与html的返回 1&#xff0c;py文件同级建立文件夹 templates 2&#xff0c;把html文件放进去 命名为hello.html html代码 <!DOCTYPE html> <html> <head><title>Hello</title> </head> <…

2024年江苏省职业院校技能大赛信息安全管理与评估 第三阶段学生组(样卷)

2024年江苏省职业院校技能大赛信息安全管理与评估 第三阶段学生组&#xff08;样卷&#xff09; 竞赛项目赛题 本文件为信息安全管理与评估项目竞赛-第三阶段样题&#xff0c;内容包括&#xff1a;网络安全渗透、理论技能与职业素养。 本次比赛时间为180分钟。 介绍 GeekSe…