LinuxCommandLibrary

builtin

Execute commands as shell builtins

TLDR

Run a shell builtin

$ builtin [command]
copy

SYNOPSIS

builtin [shell-builtin-command] [arguments...]

PARAMETERS

shell-builtin-command
    The name of the shell built-in command to execute.
Examples of shell builtins are cd, echo, and pwd.

arguments...
    The arguments to pass to the shell built-in command.

DESCRIPTION

The builtin command executes the specified shell built-in command, passing it the given arguments. This is useful when you need to run a built-in command while avoiding a shell function or external command with the same name. Using builtin ensures that you are explicitly invoking the shell's internal implementation of the command. This becomes particularly important in scripting where you want predictable behavior and avoid unintended side effects caused by aliased or overridden commands. The command returns a zero exit status unless command is not a shell built-in. If command is a shell built-in, but is invoked incorrectly, exit status is 1. Otherwise, the exit status is that of command.

CAVEATS

The builtin command only works with shell built-in commands. It will not execute external commands or shell functions. Using builtin to call user defined functions will result in a command not found error.

USE CASES

A common use case is in scripts where you want to ensure that you are using the shell's internal echo command rather than a possibly aliased version or an external echo command located in /usr/bin, to avoid unintended behavior caused by OS differences.

ERROR HANDLING

When the provided command is not a shell built-in, the builtin command exits with a non-zero status, indicating an error. This allows scripts to detect and handle cases where the intended built-in command is not available.

HISTORY

The builtin command has been a standard part of Unix-like operating systems and their shells (like Bash, Zsh, and others) for a considerable time. Its primary purpose has always been to explicitly invoke built-in shell functionalities, ensuring predictable execution, particularly in scripting contexts. It has evolved along with the shell itself, with new built-in commands added over time.

SEE ALSO

command(1), type(1)

Copied to clipboard