Builtins

Shell scripting makes extensive use of shell built-in utilities, which are part of the shell, itself. Built-ins always override the command search and execution for standalone utilities of the same name. There are two types of built-ins, regular and special.

Regular Built-ins

Regular built-ins are utilities that are provided as shell built-ins but must also be available as standalone programs that can be executed directly through mechanisms outside the shell; to execute a standalone utility instead of its built-in version, a path to the utility must be given as the command name.

Any utility can be implemented as regular built-in, but there are 17 utilities that modify the shell environment and are always implemented as built-ins due to their tight coupling with the shell. These are:

  • alias

  • bg

  • cd

  • command

  • false

  • fc

  • fg

  • getopts

  • jobs

  • kill

  • newgrp

  • pwd

  • read

  • true

  • umask

  • unalias

  • wait

Regular built-ins are overridden by functions with the same name.

Details

The bg, fg, and jobs utilities are used as part of job control to manage shell jobs, generally as part of an interactive shell session.

The alias and unalias utilities are used to manage aliases.

The cd utility is used to change the current working directory, which is used in relative pathname resolution (paths which do not begin with a slash ‘/’). The non-built-in pwd utility can be used to query the current directory; additional non-built-in utilities such as basename and dirname can be used to process path strings.

The command utility is used to explicitly ignore shell functions when performing command search and execution, in order to run actual system utilities that are shadowed by functions of the same name.

The false and true utilities are provided to avoid the overhead of searching for and executing standalone processes, since the shell can just update ? to the appropriate value.

The getopts utility is used to implement portable command-line option processing in shell scripts.

The kill utility is used to send signals to processes or process groups.

The newgrp and umask utilities are used to alter the shell session’s primary group and the file mode creation mask, which influence file access and creation.

The wait utility is used to wait on a background process or job to complete and retrieve its exit status.

Special Built-Ins

Special built-ins differ from regular built-ins in two important ways:

  1. Syntax errors in special built-ins can cause the shell to abort instead of having a non-zero exit status.

  2. Variable assignments specified with a special built-in command persist in the shell environment after the special built-in completes.

Additionally, special built-ins cannot be overridden by functions of the same name.

There are a total of 15 special built-ins:

  • break

  • colon (:)

  • continue

  • dot (.)

  • eval

  • exec

  • exit

  • export

  • readonly

  • return

  • set

  • shift

  • times

  • trap

  • unset

Note

Two special utilities have descriptive names that differ from their actual command names. The colon special built-in is invoked with the command name :, and the dot special built-in is invoked with the command name ..

Details

The break, continue, exit, and return special built-ins are used in advanced control flow. break and continue can be used to either break out, or start the next iteration, of any level of nested loops. The exit built-in causes the shell to exit with a specified exit status, while the return built-in causes the current function to complete execution with the specified exit status.

The colon (:) special built-in is the “null” utility. It is used as a placeholder when a command is needed, such as in the body of an empty loop, and always returns zero, similar to the true utility. It differs from true in that it is a special built-in with the properties described above; for example,

$ x=0
$ x=1 true
$ echo $x
0
$ x=1 :
$ echo $x
1

The dot (.) special built-in is also called the “source” utility. It accepts the name of a shell script as its argument, and executes it in the current environment, rather than in a separate process,

$ printf 'echo "My PID is: $$"; x=1;\n' > script.sh
$ x=0
$ echo "Parent PID is: $$"
Parent PID is 26753
$ sh -- script.sh
My PID is 26754
$ echo $x
0
$ . script.sh
My PID is 26753
$ echo $x
1

Note

Because a script executed with dot (.) executes in the current shell environment the exit utility causes the invoking shell to exit. A dot-sourced script can instead use the return utility to complete the current dot (.) command with a specified exit status. This is the only context where return may be used outside of a function body.

The eval special built-in is used for metaprogramming. eval can be used to perform certain tasks that are not otherwise possible. For example, variable indirection, where a variable’s value is the name of another variable, can only be portably accomplished with eval,

$ var_name=my_name
$ eval $varname=bennyb  # executes "my_name=bennyb"
$ echo $my_name
$ bennyb

The exec special built-in is used to replace the shell process with a specified command, rather than execute it as a separate child process.

The export, readonly, set, shift and unset special built-in utilities are used to manipulate shell parameters and options.

The times special built-in provides data about the accumulated user and system time (time spent executing in user space, and in system calls) that has been consumed by the shell and by its child processes since the shell was invoked.

The trap special built-in is used for managing signal handling in the shell.