如何列出所有可用的Shell内置命令?


23

我们知道bash支持许多 buitin命令, 例如:

$ type type cd help command
type is a shell builtin
cd is a shell builtin
help is a shell builtin
command is a shell builtin

我想获取所有可用的shell内置命令的列表。如何通过命令行做到这一点?



相关(用于关键字而不是内置关键字):如何列出所有shell关键字?
伊利亚·卡根

Answers:



17

您可以compgen -b在bash shell中使用它来获取shell内置命令的列表。


13

或者,您可以显示以下enable命令:(@karel和@steeldriver的答案都可以正常工作。)

enable -a | cut -d " " -f 2,3

如果禁用了任何内置功能,则显示为-n输出。

输出示例:

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

1

对于那些讨厌仅仅为了数据格式化/提取而分叉外部二进制文件的人:

while read -r _ cmd ; do echo $cmd ; done < <(enable -a)

为什么不简单地使用管道而不是FIFO重定向呢?enable -a | while read ...
David Foerster

那也可以。这只是我的一种习惯,因此我想在while循环内进行的任何变量操作都不会在子shell内执行。(在子外壳程序内操作变量将意味着更改不会持久到循环的另一端)。
Nicola Worthington

1

只需输入终端:

man bash

这将打开bash的手册。向下滚动,您将找到SHELL BUILTIN COMMANDS。在这里,您可以了解所有内置命令及其功能。如果要使用txt格式的手册,请使用此命令

man bash > FILENAME.txt

现在您有了bash manual的文本文件。


1

其它的办法: man builtins

它在顶部显示内建程序列表,然后在下面显示每个命令的所有详细信息。

SYNOPSIS
       bash defines the following built-in commands: :, ., [, alias, bg, bind,
       break,  builtin,  case,  cd,  command,  compgen,  complete,   continue,
       declare,  dirs, disown, echo, enable, eval, exec, exit, export, fc, fg,
       getopts, hash, help, history, if, jobs, kill, let, local, logout, popd,
       printf,  pushd, pwd, read, readonly, return, set, shift, shopt, source,
       suspend, test, times, trap,  type,  typeset,  ulimit,  umask,  unalias,
       unset, until, wait, while.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.