type 命令的解释

鸟哥的私房菜笔记第0章概览-linux入门

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.

显示命令的类型信息

输出每一个名字:作为命令名字他应为会被如何执行。

选项说明:

  • -a 显示所有包含名为 NAME 的可执行文件的位置; 包括别名(aliases)、内建命令(builtins)和函数(functions), 仅当未同时使用 -p 选项时生效。
  • -f 不进行 shell 函数的查找。
  • -P 强制对每个 NAME 进行 PATH 路径搜索, 即使它是别名、内建命令或函数,并返回将被执行的磁盘文件名。
  • -p 返回将被执行的磁盘文件名, 如果 type -t NAME 不返回 file,则不返回任何内容。
  • -t 输出一个单词,内容为以下之一: alias(别名),keyword(关键字),function(函数), builtin(内建命令),file(文件),或空字符串(未找到), 具体取决于 NAME 是别名、shell 保留字、shell 函数、内建命令、磁盘文件,还是未找到。

“Name 是磁盘文件” 的意思是:

当你输入的命令名(NAME)对应的是一个 磁盘上的可执行文件,也就是说这个命令不是 shell 内建命令、别名或者函数,而是在你系统某个目录(通常在 PATH 环境变量指定的路径中)中的一个实际文件,且这个文件具有执行权限。

举个例子:

  • ls 通常就是一个磁盘文件,比如 /bin/ls/usr/bin/ls
  • 当你执行 type ls,如果输出显示它是一个 file,说明 ls 命令是系统磁盘上的一个可执行文件。

所以,“Name 是磁盘文件” 就是说: 这个命令是由系统路径中某个具体的文件执行的,而不是 shell 内置功能或别名等。

发表评论

后一页 前一页