builtin Command

Execute shell built-in commands directly, bypassing aliases, functions, and external commands with the same name.

Syntax

builtin COMMAND [ARGUMENTS...]

The builtin command forces the shell to use its built-in version of a command, ignoring any aliases or functions.

Basic Usage

Execute built-in commands

# Use built-in echo (not external /bin/echo) builtin echo "Hello World" # Use built-in cd (not any cd function) builtin cd /home # Use built-in pwd builtin pwd # Use built-in test builtin test -f /etc/passwd && echo "File exists"

Execute shell built-in commands directly

Bypass aliases and functions

# Create an alias alias ls='ls --color=auto -la' # Use the alias ls # Bypass the alias (but ls is not a builtin) # This would fail: builtin ls # Example with a builtin command alias echo='echo "PREFIXED:"' echo "test" # Shows: PREFIXED: test builtin echo "test" # Shows: test

Bypass aliases to use original built-in commands

Common Built-in Commands

File and directory operations

# Change directory builtin cd /tmp builtin cd ~ builtin cd - # Print working directory builtin pwd # Directory stack operations builtin pushd /var/log builtin popd builtin dirs

Built-in commands for directory navigation

Variable and environment operations

# Set variables builtin export PATH="/usr/local/bin:$PATH" builtin export EDITOR=vim # Unset variables builtin unset TEMP_VAR # Read input builtin read -p "Enter your name: " name builtin echo "Hello, $name" # Evaluate expressions builtin eval 'echo $HOME'

Built-in commands for variables and environment

Control and execution

# Execute commands builtin exec /bin/bash # Source files builtin source ~/.bashrc builtin . ~/.profile # Exit shell builtin exit 0 # Return from function builtin return 1

Built-in commands for control flow

Testing and Conditionals

Test conditions

# File tests builtin test -f /etc/passwd && echo "File exists" builtin test -d /home && echo "Directory exists" builtin test -r ~/.bashrc && echo "File is readable" builtin test -w /tmp && echo "Directory is writable" builtin test -x /usr/bin/vim && echo "File is executable" # String tests builtin test -n "$USER" && echo "USER variable is set" builtin test -z "$EMPTY_VAR" && echo "Variable is empty" builtin test "$USER" = "root" && echo "Running as root"

Use built-in test command for conditions

Numeric comparisons

# Numeric tests num1=10 num2=20 builtin test $num1 -eq $num2 && echo "Equal" builtin test $num1 -lt $num2 && echo "$num1 is less than $num2" builtin test $num1 -gt 5 && echo "$num1 is greater than 5" builtin test $num1 -le 10 && echo "$num1 is less than or equal to 10" builtin test $num1 -ge 1 && echo "$num1 is greater than or equal to 1" builtin test $num1 -ne $num2 && echo "Not equal"

Numeric comparisons with built-in test

Job Control

Background jobs

# List jobs builtin jobs builtin jobs -l # Show process IDs builtin jobs -p # Show process IDs only builtin jobs -r # Show running jobs only builtin jobs -s # Show stopped jobs only # Bring job to foreground builtin fg %1 builtin fg # Most recent job # Send job to background builtin bg %1 builtin bg # Most recent job

Built-in job control commands

Process management

# Disable job builtin disown %1 builtin disown -h %1 # Mark to not receive SIGHUP builtin disown -a # Disown all jobs # Kill jobs builtin kill %1 builtin kill -9 %1 builtin kill -TERM %1 # Wait for jobs builtin wait %1 builtin wait # Wait for all jobs

Process management with built-ins

Practical Examples

Scripting with builtin

#!/bin/bash # Ensure we use built-in commands safe_cd() { if builtin test -d "$1"; then builtin cd "$1" builtin echo "Changed to: $(builtin pwd)" else builtin echo "Directory does not exist: $1" builtin return 1 fi } # Use the function safe_cd /tmp safe_cd /nonexistent

Use builtin in shell functions

Debugging aliases

# Check what command would be executed type echo type cd type test # Force built-in version builtin echo "This is definitely the built-in echo" # Compare outputs echo "With alias/function" builtin echo "With builtin" # List all built-in commands builtin help compgen -b # List built-ins

Debug and compare command versions

Configuration management

# Safely source configuration files safe_source() { if builtin test -f "$1" && builtin test -r "$1"; then builtin echo "Sourcing: $1" builtin source "$1" else builtin echo "Cannot source: $1" builtin return 1 fi } # Use the function safe_source ~/.bashrc safe_source ~/.custom_config # Export variables safely builtin export CUSTOM_PATH="/opt/custom/bin" builtin export PATH="$CUSTOM_PATH:$PATH"

Safe configuration management

Best Practices

builtin Usage Best Practices
  • Use builtin when you need guaranteed built-in behavior
  • Helpful in scripts where aliases might interfere
  • Use for debugging when commands behave unexpectedly
  • Combine with type command to understand command resolution
  • Useful in functions to avoid recursive calls
  • Essential for portable shell scripts
Important Considerations
  • Limited scope - Only works with actual shell built-ins
  • Shell-specific - Built-ins vary between shells
  • Not all commands - Many common commands are external
  • Portability - Built-ins may differ across systems
  • Performance - Built-ins are generally faster

See also