linux极简小知识:38、另外的帮助命令💗shell内部帮助命令help,及info查看详细信息【如何判断内部和外部命令?】

427 阅读3分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

本文已参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金。

man帮助命令、info命令,都可以查看shell内部和外部的命令!

help帮助命令查看shell内嵌命令

help command 可用于查看shell内部的命令帮助,即:命令行解释器内嵌的命令,可以使用help命令查看帮助信息。

help cd

# help cd
cd: cd [-L|[-P [-e]]] [dir]
    Change the shell working directory.

    Change the current directory to DIR.  The default DIR is the value of the
    HOME shell variable.

    The variable CDPATH defines the search path for the directory containing
    DIR.  Alternative directory names in CDPATH are separated by a colon (:).
    A null directory name is the same as the current directory.  If DIR begins
    with a slash (/), then CDPATH is not used.

    If the directory is not found, and the shell option `cdable_vars' is set,
    the word is assumed to be  a variable name.  If that variable has a value,
    its value is used for DIR.

    Options:
        -L      force symbolic links to be followed
        -P      use the physical directory structure without following symbolic
        links
        -e      if the -P option is supplied, and the current working directory
        cannot be determined successfully, exit with a non-zero status

    The default is to follow symbolic links, as if `-L' were specified.

    Exit Status:
    Returns 0 if the directory is changed, and if $PWD is set successfully when
    -P is used; non-zero otherwise.

对于shell外部命令,如果使用 help xxx 的形式,将会报错,如下:

# help mkdir
-bash: help: no help topics match `mkdir'.  Try `help help' or > `man -k mkdir' or `info mkdir'.

shell内部命令和外部命令的区分

命令行解释器(如shell),通常都有内嵌的最基本的命令,也就是shell自带的命令。这被称为内部命令,除此之外就是外部命令。

内部命令可以直接使用help命令。

外部命令,通常可以借助 --help 参数,查看帮助信息。

--help帮助参数

--help是作为查看帮助信息的参数来处理的,通常这是一个命令的标准参数。但如果,真的有命令不支持--help参数,也是无法查看的。

比如 mkdir --helpls --help等。

# mkdir --help
Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  -v, --verbose     print a message for each created directory
  -Z                   set SELinux security context of each created directory
                         to the default type
      --context[=CTX]  like -Z, or if CTX is specified then set the SELinux
                         or SMACK security context to CTX
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'mkdir invocation'

区分一个命令是shell内部命令还是外部命令?

方法一

最简单的区分方式就是,使用 help 命令,能查看帮助信息,就是内部命令;无法查看,就是外部命令。

方法二 —— type命令

shell内部命令将返回xxx is a shell builtin

# type cd
cd is a shell builtin

外部命令返回其他信息:

# type ls
ls is aliased to `ls --color=auto'
# type mkdir
mkdir is hashed (/usr/bin/mkdir)

info查看更详细的帮助信息

info可以查看更详细的帮助信息。它也是不区分shell内外部命令的,比如info lsinfo cdinfo mkdir

help、man命令还会有中文文档,显示中文。info命令的显示只有英文内容、

参考

主要参考自Linux实战技能100讲