Americas

  • United States
sandra_henrystocker
Unix Dweeb

Exploring bash builtins on Linux

How-To
May 01, 20235 mins
Linux

Here's how to learn about the many bash builtins you might be unfamiliar with.

You probably use some bash builtins fairly often whether or not you think of them as builtins or simply as commands. After all, bash builtins are commands, but not implemented as separate executables. Instead, they are part of the bash executable. In other words, they are “built into” bash, thus the term “bash builtins”.

If you’re looking for a particular builtin, the which command isn’t going to find it for you because it only looks through a collection of executables. This includes system commands like /bin/echo as well as scripts for which you have execute permission. Here’s an example of which not finding anything:

$ which help
/usr/bin/which: no help in (/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/shs/bin:/opt/pash:/home/shs)

As you can see, the which command looks down your search path ($PATH) and reports back when it can’t find a match.

Ask for a man page on any particular builtin, on the other hand, and a man page describing all of the builtins will be displayed along with explanations for each one. Just be prepared for a lot of scrolling as you scan through the output! Here’s a line count for my generated output:

$ man popd | wc -l
2109

NOTE: It doesn’t matter which particular builtin you ask about. You will get the same big helping of descriptions for all of them.

$ man shift | wc -l
2109

Ask about a builtin with the bash builtin called “help” and you’re going to get a fairly thorough explanation of what that builtin will do for you. Since help is a builtin, it can tell you about itself and any other bash builtin.

$ help help
help: help [-dms] [pattern ...]
    Display information about builtin commands.
    
    Displays brief summaries of builtin commands.  If PATTERN is
    specified, gives detailed help on all commands matching PATTERN,
    otherwise the list of help topics is printed.
    
    Options:
      -d	output short description for each topic
      -m	display usage in pseudo-manpage format
      -s	output only a short usage synopsis for each topic matching
    		PATTERN
    
    Arguments:
      PATTERN	Pattern specifying a help topic
    
    Exit Status:
    Returns success unless PATTERN is not found or an invalid option is given.

To get a list of all of the bash builtins, just type “help” on the command line and you should get a two-column listing like that shown below.

$ 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 his>
 (( expression ))                       if COMMANDS; then COMMANDS; [ elif >
 . filename [arguments]                 jobs [-lnprs] [jobspec ...] or jobs>
 :                                      kill [-s sigspec | -n signum | -sig>
 [ arg... ]                             let arg [arg ...]
 [[ expression ]]                       local [option] name[=value] ...
 alias [-p] [name[=value] ... ]         logout [n]
 bg [job_spec ...]                      mapfile [-d delim] [-n count] [-O o>
 bind [-lpsvPSVX] [-m keymap] [-f fil>  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] [>
 cd [-L|[-P [-e]] [-@]] [dir]           readarray [-d delim] [-n count] [-O>
 command [-pVv] command [arg ...]       readonly [-aAf] [name[=value] ...] >
 compgen [-abcdefgjksuv] [-o option] >  return [n]
 complete [-abcdefgjksuv] [-pr] [-DEI>  select NAME [in WORDS ... ;] do COM>
 compopt [-o|+o option] [-DEI] [name >  set [-abefhkmnptuvxBCEHPT] [-o opti>
 continue [n]                           shift [n]
 coproc [NAME] command [redirections>   shopt [-pqsu] [-o] [optname ...]
 declare [-aAfFgiIlnrtux] [name[=valu>  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] [n>  times
 eval [arg ...]                         trap [-lp] [[arg] signal_spec ...]
 exec [-cl] [-a name] [command [argum>  true
 exit [n]                               type [-afptP] name [name ...]
 export [-fn] [name[=value] ...] or e>  typeset [-aAfFgiIlnrtux] name[=valu>
 false                                  ulimit [-SHabcdefiklmnpqrstuvxPRT] >
 fc [-e ename] [-lnr] [first] [last] >  umask [-p] [-S] [mode]
 fg [job_spec]                          unalias [-a] name [name ...]
 for NAME [in WORDS ... ] ; do COMMAN>  unset [-f] [-v] [-n] [name ...]
 for (( exp1; exp2; exp3 )); do COMMA>  until COMMANDS; do COMMANDS-2; done
 function name { COMMANDS ; } or name>  variables - Names and meanings of s>
 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 ; }

Wrap-Up

There are likely many bash builtins that you use fairly often on the command line, a healthy handful that you use in scripts, and a good number that you have never tried. Maybe it’s time to explore and learn some new tricks!

sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.