注: 本文在 Manjaro Linux 下完成,其结果不一定与发行版一致,请知悉。

command types

根据 man type 得:

The standard output of type contains information about each operand in an unspecified format. The information provided typically identifies the operand as a shell built-in, function, alias or keyword, and where applicable, may display the operand's pathname.

我们得知命令有五种类型:shell built-in, function, executable binary, alias, keyword. 注意:可执行二进制文件,即:外部命令,在使用 type 时,就是显示二进制所在路径。/usr/bin 目录下存储的文件就是可执行程序。 这些可执行程序都是不同编程语言开发出来的。

  1. shell built-in 内置在 shell 中命令,也就是说是 shell 本身提供的,而不是通过 shell 调用的其他地方的二进制文件等。 例如:

    1
    2
    3
    4
    # violetv at manjaro in ~ [9:31:09]
    $ type printf read
    printf is a shell builtin
    read is a shell builtin

  2. function

    配置在环境变量中的 shell 函数,后面我们学习 shell 编程的时候就学到。

    例如:

    1
    2
    3
    4
    5
    6
    7
    8
    # violetv at manjaro in ~ [9:40:57]
    $ demoFun(){
    echo "这是我的第一个 shell 函数\!"
    }

    # violetv at manjaro in ~ [9:41:39]
    $ type demoFun
    demoFun is a shell function

  3. executable binary 可执行二进制文件,shell 调用运行的外部命令。

    1
    2
    3
    4
    # violetv at manjaro in ~ [9:44:28]
    $ type find cat
    find is /usr/bin/find
    cat is /usr/bin/cat

  4. alias 不同 shell 或者用户自定义的别名,例如:

    1
    2
    $ type ll
    ll is an alias for ls -lh

  5. reserved word (keyword)

    shell 关键字(保留字)

    1
    2
    3
    $ type for
    for is a reserved word
    ```

whatis

需要快速了解某个命令的作用,使用 whatis 命令,它通过直接显示 manual page 的第一行,来给出某个命令的简要说明

1
2
$ whatis whatis
whatis (1) - display one-line manual page descriptions

whatis 会显示命令所在的具体的文档类别,上面这个 (1) 说明命令在第一个类别。类别相关的知识在下面 man 中有详细写到。

which

通过 man which 可以得到:

It does this by searching for an executable or script in the directories listed in the environment variable PATH using the same algorithm as bash(1).

可见 which 通过搜索环境变量 PATH 列出的二进制或脚本的目录来给出指定可执行二进制文件所在的路径。

1
2
3
4
5
6
7
8
9
10
$ echo $PATH
/home/violetv/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin

# violetv at manjaro in ~ [10:13:45]
$ which python
/usr/bin/python

# violetv at manjaro in ~ [10:14:51]
$ ll /usr/bin/python
lrwxrwxrwx 1 root root 7 5月 18 02:15 /usr/bin/python -> python3

而且正因为它给出的是加入环境变量的命令的文件,因此当我们安装的软件有时不止一个版本,可能安装了好几个版本时,我们可以通过 which cmd 找到我们运行该软件的时候,默认执行的版本。

1
2
$ ll `which python`
lrwxrwxrwx 1 root root 7 5月 18 02:15 /usr/bin/python -> python3

使用演示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# violetv at manjaro in ~ [10:09:17]
$ which cat
/usr/bin/cat

# violetv at manjaro in ~ [10:09:58]
$ which read
read: shell built-in command

# violetv at manjaro in ~ [10:10:03]
$ which for
for: shell reserved word

# violetv at manjaro in ~ [10:10:09]
$ which demoFun
demoFun () {
echo "这是我的第一个 shell 函数!"
}

# violetv at manjaro in ~ [10:10:17]
$ which ll
ll: aliased to ls -lh

whereis

通过 man whereis 我们发现 Description 中有这样两句话:

whereis locates the binary, source and manual files for the specified command names.

whereis then attempts to locate the desired program in the standard Linux places, and in the places specified by $PATH and $MANPATH.

whereis 通过搜索 the standard Linux places$PATH$MANPATH 列出的位置来进行定位,主要定位三类文件: binary, source and manual files

两个使用场景:

  1. 安装了多个版本的 python 之类的,看一下安装了哪些具体的版本号
  2. 查看 python 库文件位置,例如: usr/lib/python3.8 之类的

man

Man pages are the UNIX traditional way of distributing documentation about programs. The term "man page" itself is short for "manual page", as they correspond to the pages of the printed manual; the man pages "sections" (1 for commands, 2 for system calls, etc.) correspond to sections in the full UNIX manual.

在 man 的帮助手册中,将帮助文档分为了 9 个类别,对于有的关键字可能存在多个类别中, 我们就需要指定特定的类别来查看;(一般我们查询 bash 命令,归类在 1 类中);

man 页面所属的分类标识(常用的是分类 1 和分类 3)

(1)、用户可以操作的命令或者是可执行文件 (2)、系统核心可调用的函数与工具等 (3)、一些常用的函数与数据库 (4)、设备文件的说明 (5)、设置文件或者某些文件的格式 (6)、游戏 (7)、惯例与协议等。例如 Linux 标准文件系统、网络协议、ASCⅡ,码等说明内容 (8)、系统管理员可用的管理条令 (9)、与内核有关的文件

通过 man man 我们发现:

The table below shows the section numbers of the manual followed by the types of pages they contain.

1 Executable programs or shell commands(可执行程序或 shell 命令) 2 System calls (functions provided by the kernel)(程序接口内核系统调用) 3 Library calls (functions within program libraries)(C 库函数程序接口) 4 Special files (usually found in /dev)(特殊文件,比如说驱动程序和设备文件 /dev/null, /dev/zero 之类的) 5 File formats and conventions, e.g. /etc/passwd (文件格式) 6 Games(游戏娱乐,如屏幕保护程序) 7 Miscellaneous (including macro package and conventions), e.g. man(7), groff(7)(杂项) 8 System administration commands (usually only for root)(系统管理员命令) 9 Kernel routines [Non standard] (内核惯例)

指定类别

当我们需要指定特定的类别来查看, 命令: man [1-8] program

例如直接 man printf 它会默认显示第一个存在该命令的类别,因此显示的是第一个类别 shell 命令中的 printf。如果你希望查看 c 语言的 printf 帮助,则需要 man 3 printf

一般我们可以通过 whatis 查看命令都处在哪些类别,然后通过 man 指定类别详细看

1
2
3
4
5
6
$ whatis printf
printf (3) - formatted output conversion
printf (3p) - print formatted output
printf (1p) - write formatted output
printf (1) - format and print data
$ man 3 printf

适用场景:第一次学习使用某个命令,因为内容比较详细。

info

In the early '90s, the GNU project decided that "man" documentation system was outdated, and wrote the info command to replace it: info has basic hyperlinking features and a simpler markup language to use (compared to the "troff" system used for man pages). In addition, GNU advocates against the use of man pages at all and contends that complex software systems should have complete and comprehensive documentation rather than just a set of short man pages.

适用场景:第一次学习使用某个命令,因为内容比 man 还详细。

man vs info

They are two different documentation systems. There are actually other documentation systems in use, besides man and info: GNOME and KDE have their own, HTML-based system, etc.

Differences

  • Keybinding man uses less to display the manual page which in turn uses vim keybindings while info uses emacs keybindings and standard emacs commands for search, quitting etc.

  • Period

    Info is the default format for documentation inside the GNU project, man is the much older traditional format for UNIX.

  • Format

    Info uses Texinfo as its source format, which is a bunch of macros for TeX, and that makes it much easier to also create "good-looking" printed versions or PDFs. Generally, man pages are more about 1 specific topic (a command, a config file, a system function, ...) while info normally has more the structure of a combined manual, more like a book.

    Man is giving complete command options and descriptions. info command also similar to man but info command listing structured level. We can easily read and understand the command options

  • Content

    The main difference between Man and Info is the amount of content that they have; Info contains a whole lot more than Man does.

  • Which one?

    Some people like Info because they can get a lot more from it while others like Man because it is straight to the point and they can quickly get what they want without wasting time.

--help 选项

--help 选项并不是所有命令都有的,不过,根据我使用的经验来看,大部分外部命令都是有的。它显示的信息比 man 精简一些

适用场景: 学过该命令,但是忘了某个选项,此时,使用 --help 简洁明了。

参考资料

1. 学会使用命令帮助 which/whereis differences Linux Type Command What is the difference between “man” and “info” documentation? Difference between help, info and man command