获取有关 Linux shell 内置命令的帮助
创始人
2024-03-02 03:14:34
0

Linux 内置命令属于用户 shell 的一部分,本文将告诉你如何识别它们并获取使用它们的帮助。

Linux 内置命令是内置于 shell 中的命令,很像内置于墙中的书架。与标准 Linux 命令存储在 /usr/bin 中的方式不同,你不会找到它们的独立文件,你可能使用过相当多的内置命令,但你不会感觉到它们与 lspwd 等命令有何不同。

内置命令与其他 Linux 命令一样使用,它们可能要比不属于 shell 的类似命令运行得快一些。Bash 内置命令包括 aliasexportbg 等。

正如你担心的那样,因为内置命令是特定于 shell 的,所以它们不会提供手册页。使用 man 来查看 bg,你会看到这样的东西:

$ man bg
No manual entry for bg

判断内置命令的另一个提示是当你使用 which 命令来识别命令的来源时,Bash 不会响应,表示没有与内置命令关联的文件:

$ which bg
$

另一方面,如果你的 shell 是 /bin/zsh,你可能会得到一个更有启发性的响应:

% which bg
bg: shell built-in command

bash 提供了额外的帮助信息,但它是通过使用 help 命令实现的:

$ help bg
bg: bg [job_spec ...]
    Move jobs to the background.

    Place the jobs identified by each JOB_SPEC in the background, as if they
    had been started with `&'.  If JOB_SPEC is not present, the shell's notion
    of the current job is used.

    Exit Status:
    Returns success unless job control is not enabled or an error occurs.

如果你想要查看 bash 提供的所有内置命令的列表,使用 compgen -b 命令。通过管道将命令输出到列中,以获得较好格式的清单。

$ compgen -b | column
.              compgen        exit           let            return         typeset
:              complete       export         local          set            ulimit
[              compopt        false          logout         shift          umask
alias          continue       fc             mapfile        shopt          unalias
bg             declare        fg             popd           source         unset
bind           dirs           getopts        printf         suspend        wait
break          disown         hash           pushd          test
builtin        echo           help           pwd            times
caller         enable         history        read           trap
cd             eval           jobs           readarray      true
command        exec           kill           readonly       type

如果你使用 help 命令,你将看到一个内置命令列表以及简短描述。但是,这个列表被截断了(以 help 命令结尾):

$ help
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
These shell commands are defined internally.  Type `help' to see this list.
Type `help name' to find out more about the function `name'.
Use `info bash' to find out more about the shell in general.
Use `man -k' or `info' to find out more about commands not in this list.

A star (*) next to a name means that the command is disabled.

 job_spec [&]                             history [-c] [-d offset] [n] or histo>
 (( expression ))                         if COMMANDS; then COMMANDS; [ elif CO>
 . filename [arguments]                   jobs [-lnprs] [jobspec ...] or jobs ->
 :                                        kill [-s sigspec | -n signum | -sigsp>
 [ arg... ]                               let arg [arg ...]
 [[ expression ]]                         local [option] name[=value] ...
 alias [-p] [name[=value] ... ]           logout [n]
 bg [job_spec ...]                        mapfile [-d delim] [-n count] [-O ori>
 bind [-lpsvPSVX] [-m keymap] [-f filen>  popd [-n] [+N | -N]
 break [n]                                printf [-v var] format [arguments]
 builtin [shell-builtin [arg ...]]        pushd [-n] [+N | -N | dir]
 caller [expr]                            pwd [-LP]
 case WORD in [PATTERN [| PATTERN]...) >  read [-ers] [-a array] [-d delim] [-i>
 cd [-L|[-P [-e]] [-@]] [dir]             readarray [-d delim] [-n count] [-O o>
 command [-pVv] command [arg ...]         readonly [-aAf] [name[=value] ...] or>
 compgen [-abcdefgjksuv] [-o option] [->  return [n]
 complete [-abcdefgjksuv] [-pr] [-DEI] >  select NAME [in WORDS ... ;] do COMMA>
 compopt [-o|+o option] [-DEI] [name ..>  set [-abefhkmnptuvxBCHP] [-o option-n>
 continue [n]                             shift [n]
 coproc [NAME] command [redirections]     shopt [-pqsu] [-o] [optname ...]
 declare [-aAfFgilnrtux] [-p] [name[=va>  source filename [arguments]
 dirs [-clpv] [+N] [-N]                   suspend [-f]
 disown [-h] [-ar] [jobspec ... | pid . <p>'>  test [expr]
 echo [-neE] [arg ...]                    time [-p] pipeline
 enable [-a] [-dnps] [-f filename] [nam>  times
 eval [arg ...]                           trap [-lp] [[arg] signal_spec ...]
 exec [-cl] [-a name] [command [argumen>  true
 exit [n]                                 type [-afptP] name [name ...]
 export [-fn] [name[=value] ...] or exp>  typeset [-aAfFgilnrtux] [-p] name[=va>
 false                                    ulimit [-SHabcdefiklmnpqrstuvxPT] [li>
 fc [-e ename] [-lnr] [first] [last] or>  umask [-p] [-S] [mode]
 fg [job_spec]                            unalias [-a] name [name ...]
 for NAME [in WORDS ... ] ; do COMMANDS>  unset [-f] [-v] [-n] [name ...]
 for (( exp1; exp2; exp3 )); do COMMAND>  until COMMANDS; do COMMANDS; done
 function name { COMMANDS ; } or name (>  variables - Names and meanings of som>
 getopts optstring name [arg]             wait [-fn] [id ...]
 hash [-lr] [-p pathname] [-dt] [name .>  while COMMANDS; do COMMANDS; done
 help [-dms] [pattern ...]                { COMMANDS ; }

从上面的清单中可以看出,help 命令本身就是内置的。

你可以通过向 help 命令提供你感兴趣的内置命令名称来获取关于它们的更多信息,例如 help dirs

$ help dirs
dirs: dirs [-clpv] [+N] [-N]
    Display directory stack.

    Display the list of currently remembered directories.  Directories
    find their way onto the list with the `pushd' command; you can get
    back up through the list with the `popd' command.

    Options:
      -c        clear the directory stack by deleting all of the elements
      -l        do not print tilde-prefixed versions of directories relative
                to your home directory
      -p        print the directory stack with one entry per line
      -v        print the directory stack with one entry per line prefixed
                with its position in the stack

    Arguments:
      +N        Displays the Nth entry counting from the left of the list
                shown by dirs when invoked without options, starting with
                zero.

      -N        Displays the Nth entry counting from the right of the list
                shown by dirs when invoked without options, starting with
                zero.

    Exit Status:
    Returns success unless an invalid option is supplied or an error occurs.

内置命令提供了每个 shell 的大部分功能。你使用的任何 shell 都有一些内置命令,但是如何获取这些内置命令的信息可能因 shell 而异。例如,对于 zsh,你可以使用 man zshbuiltins 命令获得其内置命令的描述。

$ man zshbuiltins

ZSHBUILTINS(1)               General Commands Manual              ZSHBUILTINS(1)

NAME
       zshbuiltins - zsh built-in commands

SHELL BUILTIN COMMANDS
       Some  shell  builtin commands take options as described in individual en‐
       tries; these are often referred to in the list below as `flags' to  avoid
       confusion with shell options, which may also have an effect on the behav‐
       iour of builtin commands.  In this introductory section, `option'  always
       has the meaning of an option to a command that should be familiar to most
       command line users.
…

在这个冗长的手册页中,你将找到一个内置命令列表,其中包含有用的描述,如下摘录中所示:

bg [ job ... ]
job ... &
       Put  each  specified  job in the background, or the current job if
       none is specified.

bindkey
       See the section `Zle Builtins' in zshzle(1).

break [ n ]
       Exit from an enclosing for, while, until, select or  repeat  loop.
       If  an  arithmetic  expression n is specified, then break n levels
       instead of just one.

最后

Linux 内置命令对于每个 shell 都很重要,它的操作类似特定于 shell 的命令一样。如果你经常使用不同的 shell,并注意到你经常使用的某些命令似乎不存在或者不能按预期工作,那么它可能是你使用的其他 shell 之一中的内置命令。


via: https://www.networkworld.com/article/3410350/getting-help-for-linux-shell-built-ins.html

作者:Sandra Henry-Stocker 选题:lujun9972 译者:MjSeven 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关内容

大数据开发中常用的shel...
内容目录一、date二、find三、awk四、cut五、if六、s...
2025-05-31 22:29:07
shell常见面试题二
(8)、shell中case语法 #!...
2025-05-30 22:47:54
Shell 的变量功能
什么是变量?? 应该很好理解吧&#x...
2025-05-30 15:50:47
shell常见面试题三
(16)、read读取终端输入 &#x...
2025-05-30 13:17:49
Web漏洞-XSS跨站之订...
一、XSS盲打可测试位置留言板、评论区、订单系统、反馈条件等位置。...
2025-05-29 08:31:04
shell命令如何正确调用...
前言 本文是该专栏的第20篇,后面会持续分享python的各种干...
2025-05-29 06:53:20

热门资讯

Helix:高级 Linux ... 说到 基于终端的文本编辑器,通常 Vim、Emacs 和 Nano 受到了关注。这并不意味着没有其他...
使用 KRAWL 扫描 Kub... 用 KRAWL 脚本来识别 Kubernetes Pod 和容器中的错误。当你使用 Kubernet...
JStock:Linux 上不... 如果你在股票市场做投资,那么你可能非常清楚投资组合管理计划有多重要。管理投资组合的目标是依据你能承受...
通过 SaltStack 管理... 我在搜索Puppet的替代品时,偶然间碰到了Salt。我喜欢puppet,但是我又爱上Salt了:)...
Epic 游戏商店现在可在 S... 现在可以在 Steam Deck 上运行 Epic 游戏商店了,几乎无懈可击! 但是,它是非官方的。...
《Apex 英雄》正式可在 S... 《Apex 英雄》现已通过 Steam Deck 验证,这使其成为支持 Linux 的顶级多人游戏之...
如何在 Github 上创建一... 学习如何复刻一个仓库,进行更改,并要求维护人员审查并合并它。你知道如何使用 git 了,你有一个 G...
2024 开年,LLUG 和你... Hi,Linuxer,2024 新年伊始,不知道你是否已经准备好迎接新的一年~ 2024 年,Lin...
什么是 KDE Connect... 什么是 KDE Connect?它的主要特性是什么?它应该如何安装?本文提供了基本的使用指南。科技日...
Opera 浏览器内置的 VP... 昨天我们报道过 Opera 浏览器内置了 VPN 服务,用户打开它可以防止他们的在线活动被窥视。不过...