builtin - Linux


Overview

The builtin command in Linux is used to execute a shell builtin, passing it arguments, and returning its exit status. This command is integral for explicitly running built-in commands while avoiding external command or function invocations with the same name. It’s primarily useful in scripting and shell customization to ensure that the intended built-in command is executed rather than any overridden version.

Syntax

builtin [shell-builtin [args]]
  • shell-builtin: Specifies the name of the shell built-in you intend to use.
  • args: Arguments or options passed to the built-in.

Options/Flags

The builtin command typically does not have its own options aside from the shell built-ins’ arguments. The behavior and accepted parameters entirely depend on the built-in command being invoked.

Examples

  1. Using command to bypass aliases and functions:
    You might have an alias or function that overrides a default command. Using builtin you can access the original functionality:

    builtin echo "This will use the built-in echo command, ignoring any alias or function named 'echo'."
    
  2. Complex script integration:
    When writing scripts that should behave consistently regardless of the user’s environment, use builtin to ensure that your script uses the shell’s built-in functionality:

    for i in 1 2 3; do builtin echo $i; done
    

Common Issues

  • Command Not Found: If you try to use builtin with a command that is not a shell built-in, you will get an error like bash: builtin: ls: not a shell builtin.
  • Misunderstanding Scope: builtin only affects shell built-in commands. It does not bypass the PATH for external commands that are not built into the shell.

Integration

builtin can be coupled with other commands in scripts to ensure reliability across different environments where aliases or functions might shadow expected command behavior:

if builtin test -f "$FILE"; then
    builtin echo "$FILE exists."
fi

This script checks for the existence of a file using the built-in test and echo commands, ensuring that the check isn’t interfered with by overridden commands.

  • type: Displays information about the command type. Useful to check if a command is a built-in, file, alias, or function.
  • command: Similar to builtin, but can also execute commands in the PATH and ignore functions.

Further reading: You can usually find more about built-in commands in the shell’s manual page (man bash for Bash).

Using the builtin command effectively ensures control and predictability in scripts, making sure that expected shell built-in functionalities are used without interference from user-defined functions or aliases.