- 구글 클라우드, 구글 워크스페이스용 제미나이 사이드 패널에 한국어 지원 추가
- The best MagSafe accessories of 2024: Expert tested and reviewed
- Threads will show you more from accounts you follow now - like Bluesky already does
- OpenAI updates GPT-4o, reclaiming its crown for best AI model
- Nile unwraps NaaS security features for enterprise customers
Using the Linux compgen bash builtin
Linux’s compgen command is not actually a Linux command. In other words, it’s not implemented as an executable file, but is instead a bash builtin. That means that it’s part of the bash executable. So, if you were to type “which compgen”, your shell would run through all of the locations included in your $PATH variable, but it just wouldn’t find it.
$ which compgen /usr/bin/which: no compgen in (.:/home/shs/.local/bin:/home/shs/bin:/usr/local/bin: /usr/bin:/usr/local/sbin:/usr/sbin)
Obviously, the which command had no luck in finding it.
If, on the other hand, you type “man compgen”, you’ll end up looking at the man page for the bash shell. From that man page, you can scroll down to this explanation if you’re patient enough to look for it.
Here’s what you should look for …
compgen [option] [word] Generate possible completion matches for word according to the options, which may be any option accepted by the complete builtin with the exception of -p and -r, and write the matches to the standard output. When using the -F or -C options, the various shell variables set by the programmable com‐ pletion facilities, while available, will not have useful values. The matches will be generated in the same way as if the programmable comple‐ tion code had generated them directly from a completion specification with the same flags. If word is specified, only those completions matching word will be displayed. The return value is true unless an invalid option is supplied, or no matches were generated.
Typing “help compgen” will give you a brief description of just the compgen command:
$ help compgen compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word] Display possible completions depending on the options. Intended to be used from within a shell function generating possible completions. If the optional WORD argument is supplied, matches against WORD are generated. Exit Status: Returns success unless an invalid option is supplied or an error occurs.
Listing commands with compgen
Basically, compgen is a bash builtin which is used to provide different varieties of information. For example, it can list all the commands that could be executed on the Linux system. To get a list of all commands available for direct execution, use a command like the one shown below – but expect a lot of output than what you see here when you do! The output below is seriously truncated.
$ comgen -c | column big5 pkaction c pkcheck egrep pkexec fgrep pkttyagent grep virt-admin install virt-host-validate l. virt-ssh-helper ll nm-online ls nmcli myprocs udisksctl myps cd-create-profile rec cd-fix-profile update cd-iccdump which cd-it8 xzegrep colormgr
The command below illustrates how many lines of output would be generated by the command shown above if not truncated:
$ compgen -c | wc -l 2693
Listing bash builtins with compgen
You can also use compgen to list all of the bash builtins. I like to pass the output to the column command to make it easier to review.
$ compgen -b | column . cd echo getopts mapfile set type : command enable hash popd shift typeset [ compgen eval help printf shopt ulimit alias complete exec history pushd source umask bg compopt exit jobs pwd suspend unalias bind continue export kill read test unset break declare false let readarray times wait builtin dirs fc local readonly trap caller disown fg logout return true
You can also use the help command to list bash builtins like this:
$ help GNU bash, version 5.2.15(1)-release (x86_64-redhat-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 history -a> (( expression )) if COMMANDS; then COMMANDS; [ elif COMMAND> . filename [arguments] jobs [-lnprs] [jobspec ...] or jobs -x com> : kill [-s sigspec | -n signum | -sigspec] p> [ arg... ] let arg [arg ...] [[ expression ]] local [option] name[=value] ... alias [-p] [name[=value] ... ] logout [n] bg [job_spec ...] mapfile [-d delim] [-n count] [-O origin] > bind [-lpsvPSVX] [-m keymap] [-f filename] > 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]...) COMMA> read [-ers] [-a array] [-d delim] [-i text> cd [-L|[-P [-e]] [-@]] [dir] readarray [-d delim] [-n count] [-O origin> command [-pVv] command [arg ...] readonly [-aAf] [name[=value] ...] or read> compgen [-abcdefgjksuv] [-o option] [-A act> return [n] complete [-abcdefgjksuv] [-pr] [-DEI] [-o o> select NAME [in WORDS ... ;] do COMMANDS; > compopt [-o|+o option] [-DEI] [name ...] set [-abefhkmnptuvxBCEHPT] [-o option-name> continue [n] shift [n] coproc [NAME] command [redirections] shopt [-pqsu] [-o] [optname ...] declare [-aAfFgiIlnrtux] [name[=value] ...]> source filename [arguments] dirs [-clpv] [+N] [-N] suspend [-f] disown [-h] [-ar] [jobspec ... | pid ...] test [expr] echo [-neE] [arg ...] time [-p] pipeline enable [-a] [-dnps] [-f filename] [name ...> times eval [arg ...] trap [-lp] [[arg] signal_spec ...] exec [-cl] [-a name] [command [argument ...> true exit [n] type [-afptP] name [name ...] export [-fn] [name[=value] ...] or export -> typeset [-aAfFgiIlnrtux] name[=value] ... > false ulimit [-SHabcdefiklmnpqrstuvxPRT] [limit] fc [-e ename] [-lnr] [first] [last] or fc -> umask [-p] [-S] [mode] fg [job_spec] unalias [-a] name [name ...] for NAME [in WORDS ... ] ; do COMMANDS; don> unset [-f] [-v] [-n] [name ...] for (( exp1; exp2; exp3 )); do COMMANDS; do> until COMMANDS; do COMMANDS-2; done function name { COMMANDS ; } or name () { C> variables - Names and meanings of some she> getopts optstring name [arg ...] wait [-fn] [-p var] [id ...] hash [-lr] [-p pathname] [-dt] [name ...] while COMMANDS; do COMMANDS-2; done help [-dms] [pattern ...] { COMMANDS ; }
Listing your aliases with compgen
To get compgen to list your aliases, use the -a option like this:
$ compgen -a | column big5 egrep grep l. ls myps update xzegrep xzgrep zfgrep c fgrep install ll myprocs rec which xzfgrep zegrep zgrep
Of course, you could also use the alias command – probably easier to remember — and see the command that each alias is associated with.
$ alias alias big5='du -h | sort -h | tail -5' alias c="clear" alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias install="sudo dnf install" alias l.='ls -d .* --color=auto' alias ll="ls -l --color=auto" alias ls="ls --color=auto" alias myprocs="ps -ef | grep shs" alias myps="ps -ef | grep shs | awk "''{print $2}''' ' alias rec="ls -ltr | tail -3" alias update="sudo dnf upgrade –refresh" alias which="(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot" alias xzegrep='xzegrep --color=auto' alias xzfgrep='xzfgrep --color=auto' alias xzgrep='xzgrep --color=auto' alias zegrep='zegrep --color=auto' alias zfgrep='zfgrep --color=auto' alias zgrep='zgrep --color=auto'
Wrap-up
The compgen command can come in very handy, but it has a lot of options to use and determine which are the most useful ones. With very brief explanations, they are listed below:
a == aliases b == builtins c == commands d == directories in current file system position e == exported shell variables f == files g == groups j == jobs k == shell reserved words s == services u == user names v == shell variables
Copyright © 2023 IDG Communications, Inc.