Bash’s built in commands

If you’re a frequent user of the bash shell, I would suggest taking a peek at the GNU reference guide next time you have a chance.  There are a lot of cool built in functions/commands within bash that are pretty neat.  To get an idea of what these built in commands are:

$ ps
15997 pts/2    00:00:00 bash
23625 pts/2    00:00:00 ps

$ which enable
/usr/bin/which: no enable in (/bin:/usr/bin:/usr/sbin:/sbin)

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

Some of these do have binaries within the /usr or /bin namespace, while others do not.  Bash’s internal built in definition of these commands is what actually gets executed…

$ which cd
/usr/bin/which: no cd in (/bin:/usr/bin:/usr/sbin:/sbin)
$ which echo
$ which eval
/usr/bin/which: no eval in (/bin:/usr/bin:/usr/sbin:/sbin)
$ which exit
/usr/bin/which: no exit in (/bin:/usr/bin:/usr/sbin:/sbin)
$ which kill

Some of these are pretty intuitive why they should be built-in commands (cd, exit, etc.)  Looking through the GNU reference guide describes what all of these do.  There is also a built-in called “help” which describes some of this without going to the GNU reference guide.

$ which help
/usr/bin/which: no help in (/bin:/usr/bin:/usr/sbin:/sbin)
$ help cd
cd: cd [-L|-P] [dir]
Change the current directory to DIR.  The variable $HOME is the
default DIR.  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, i.e. `.’.  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, then try the word as a variable
name.  If that variable has a value, then cd to the value of that
variable.  The -P option says to use the physical directory structure
instead of following symbolic links; the -L option forces symbolic links
to be followed.
I found the “bind” built in to be super useful.  Bash has shortcuts that allow you to move the cursor around, search history, etc. and I always find myself forgetting what the keyboard shortcut / sequence is.  When this happens, I just use the “bind” (not DNS!) built in to find what I’m looking for…

$ bind -p

Displays all “bindings” of keyboard shortcuts to variables.   Here, I want to see what keyboard shortcut I can use to search through my command history to find a command…

$ bind -p | grep reverse-search-history
“\C-r”: reverse-search-history

Ah, so its Ctrl-r..  Sure enough, hitting ctrl-r at a command prompt brings up the reverse search prompt through my history and I can look for any command…  Looking for the command “cat” seems to have found me looking through /etc/passwd.


(reverse-i-search)`cat’: cat /etc/passwd

Hitting ctrl-r multiple times once command is found will continue to search backwards through the history.

(reverse-i-search)`cat’: cat /proc/kallsyms | more

Reverse history search is pretty useful, but there are so many other neat features of bash that I find invaluable.  What if I have created some super long command with multiple pipes and I want to move the cursor to the beginning of the line?

$ bind -p | grep beginning-of-line
“\C-a”: beginning-of-line
“\eOH”: beginning-of-line
“\e[1~”: beginning-of-line
“\e[H”: beginning-of-line

Well, it looks like there are a few defined shortcuts, but ctrl-a seems to be a winner.  Sure enough..

$ cat /etc/passwd | awk -F: ‘{print $7}’ | sed ‘s,bin,mike,'[]

$ [c]at /etc/passwd | awk -F: ‘{print $7}’ | sed ‘s,bin,mike,’

Take a peek!  You might find yourself saving some keystrokes.

$ help bind
bind: bind [-lpvsPVS] [-m keymap] [-f filename] [-q name] [-u name] [-r keyseq] [-x keyseq:shell-command] [keyseq:readline-function or readline-command]
Bind a key sequence to a Readline function or a macro, or set
a Readline variable.  The non-option argument syntax is equivalent
to that found in ~/.inputrc, but must be passed as a single argument:
bind ‘”\C-x\C-r”: re-read-init-file’.
bind accepts the following options:
-m  keymap         Use `keymap’ as the keymap for the duration of this
command.  Acceptable keymap names are emacs,
emacs-standard, emacs-meta, emacs-ctlx, vi, vi-move,
vi-command, and vi-insert.
-l                 List names of functions.
-P                 List function names and bindings.
-p                 List functions and bindings in a form that can be
reused as input.
-r  keyseq         Remove the binding for KEYSEQ.
-x  keyseq:shell-command  Cause SHELL-COMMAND to be executed when
KEYSEQ is entered.
-f  filename       Read key bindings from FILENAME.
-q  function-name  Query about which keys invoke the named function.
-u  function-name  Unbind all keys which are bound to the named function.
-V                 List variable names and values
-v                 List variable names and values in a form that can
be reused as input.
-S                 List key sequences that invoke macros and their values
-s                 List key sequences that invoke macros and their values
in a form that can be reused as input.

Want to see if your current shell has history enabled, or some other key bash features?  The GNU Bash Reference guide has details on what all of these variables are.. Like checkwinsize.

$ shopt
cdable_vars     off
cdspell         off
checkhash       off
checkwinsize    on
cmdhist         on
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         off
extquote        on
failglob        off
force_fignore   on
gnu_errfmt      off
histappend      off
histreedit      off
histverify      off
hostcomplete    on
huponexit       off
interactive_comments    on
lithist         off
login_shell     on
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off
$ help shopt
shopt: shopt [-pqsu] [-o long-option] optname [optname…]
Toggle the values of variables controlling optional behavior.
The -s flag means to enable (set) each OPTNAME; the -u flag
unsets each OPTNAME.  The -q flag suppresses output; the exit
status indicates whether each OPTNAME is set or unset.  The -o
option restricts the OPTNAMEs to those defined for use with
`set -o’.  With no options, or with the -p option, a list of all
settable options is displayed, with an indication of whether or
not each is set.

If set, Bash checks the window size after each command and, if
necessary, updates the values of LINES and COLUMNS.

Ah nice.  My shell wont send a SIGHUP to jobs while i’m logging out of an interactive shell.  =)

1 thought on “Bash’s built in commands”

Leave a Reply

Your email address will not be published. Required fields are marked *