LinuxCommandLibrary

builtin

Execute commands as shell builtins

TLDR

Run a shell builtin

$ builtin [command]
copy

SYNOPSIS

builtin command_name [arg...]

DESCRIPTION

The builtin command is a special shell construct used to execute a specific shell builtin command.

A shell builtin is a command implemented directly within the shell itself, rather than being a separate executable program found on the file system (like /bin/ls). Examples include cd, pwd, echo, exit, and test.

The primary purpose of the builtin command is to ensure that a builtin version of a command is executed, even if a user-defined function or an external executable with the same name exists in the shell's search path. This allows for bypassing shadowing by aliases, functions, or external commands.

Using builtin offers several advantages: performance, as it avoids the overhead of forking a new process; and reliability, as it guarantees the execution of the shell's intrinsic, trusted version of a command. It is distinct from the command builtin, which also bypasses functions and aliases but then searches for external executables as well, whereas builtin exclusively targets shell internal commands.

CAVEATS

The builtin command will only succeed if the specified command_name is indeed a true shell builtin. If command_name is an alias, a function, or an external executable, builtin will return an error status and print an error message (e.g., "command not a shell builtin").

Its behavior and available builtins are specific to the shell interpreter (e.g., Bash, Zsh, Ksh) in which it is executed, although the core functionality is universally similar.

PERFORMANCE AND EFFICIENCY

Executing a shell builtin via builtin avoids the overhead of forking a new process (which is typically required for external commands). This makes builtins significantly faster and more resource-efficient for common shell operations like changing directories (cd) or printing the current directory (pwd).

SECURITY AND RELIABILITY

By explicitly calling a builtin, you ensure that the shell's trusted, internal implementation of a command is used. This can be crucial in scripting environments where you need to guarantee consistent behavior, especially for commands like exit or test, preventing malicious or unintended external programs or functions from being executed instead.

HISTORY

The concept of shell builtins and the ability to explicitly invoke them has been a fundamental part of Unix shells since their early development. The builtin command itself is deeply integrated into modern shells like Bash, Zsh, and Ksh, rather than being a standalone development project. It emerged as a necessary mechanism to manage the hierarchy of command execution (aliases > functions > builtins > executables) and ensure shell integrity and efficiency. Its design has remained largely consistent due to its foundational role.

SEE ALSO

command(1), enable(1), type(1), hash(1)

Copied to clipboard