Bash Built-in Commands vs. External Commands

Su Suriya Ravichandran Updated 06 Dec 2026
8 min read ·Lesson 14 of 205

Not every command you type is a separate program. Some, like cd, are part of Bash itself, while others, like ls, are programs stored as files on disk. Knowing which is which explains why certain commands behave the way they do, why some have two different help systems, and why you can sometimes replace one command with your own version.

Understanding the Concept

When Bash runs a command, it can take one of several routes:

  • A built-in command is code inside the Bash program. Bash runs it directly, with no new process. Examples are cd, echo, read, export, and type.
  • An external command is a separate executable file stored on disk, such as /usr/bin/ls or /usr/bin/grep. Bash starts it as a new process, waits for it to finish, and reads its exit status.
  • A shell keyword is a reserved word that Bash treats as part of its own grammar, such as if, for, while, and case.
  • An alias or a function is a name you define yourself that expands into other commands.

From your point of view, they all look the same: a word followed by arguments. The difference lies in where the code lives and how Bash finds it.

Why Both Kinds Exist

Built-ins exist for two main reasons.

Some tasks can only be done by the shell itself. Each program runs in its own process, and a process cannot change the state of the process that started it. The command cd must change the working directory of the shell, so it cannot be a separate program. If it were, it would change its own directory and then exit, leaving your shell where it was. You can watch this happen:

(cd /tmp; pwd)
pwd

Output, assuming you started in /home/student:

/tmp
/home/student

The parentheses start a temporary subshell, which is a child copy of the shell. Inside it, cd really does move to /tmp. When the subshell ends, your original shell is still in its old directory. The same thing happens with an external Bash process:

bash -c 'cd /usr; pwd'
pwd

Output:

/usr
/home/student

The bash -c program ran its own cd, printed /usr, and then exited. Your own shell never moved. This is the reason cd, export, exit, and source are built-ins.

Built-ins are faster. Starting a process costs time. For small, frequently used commands, such as echo or test, avoiding the process start makes scripts noticeably quicker when they run thousands of times.

External commands exist because the system ships thousands of tools, and putting them all inside Bash would be impractical. Programs like ls, grep, sort, and curl are separate, so they can be updated, replaced, or written in any language.

How Bash Finds a Command

When you type a name, Bash looks for it in a fixed order and uses the first match:

  1. An alias with that name.
  2. A shell keyword, such as if.
  3. A function defined in the current shell.
  4. A built-in command.
  5. An external program found by searching the directories listed in the PATH variable, in order.

The PATH variable holds a colon-separated list of directories:

echo "$PATH"

Sample output:

/usr/local/bin:/usr/bin:/bin

Bash checks /usr/local/bin first, then /usr/bin, then /bin. The first directory that contains an executable file with the right name wins. After it finds an external program, Bash remembers the location in a small table so that it can skip the search next time.

Finding Out What a Command Is

The type built-in tells you how Bash will interpret a name:

type cd
type ls
type if

Sample output:

cd is a shell builtin
ls is /usr/bin/ls
if is a shell keyword

Each line reports the category. For an external command, type also prints the file that Bash will run. If you only need the category word, use type -t:

type -t cd
type -t ls
type -t if

Output:

builtin
file
keyword

The words are alias, keyword, function, builtin, or file. The -t form is helpful inside scripts, because the output is a single predictable word.

Seeing Every Match

A name can exist in several places at once. Use type -a to list them all, in the order Bash would consider them:

type -a pwd

Sample output:

pwd is a shell builtin
pwd is /usr/bin/pwd
pwd is /bin/pwd

Both a built-in pwd and an external /usr/bin/pwd exist. Bash uses the first one, the built-in. On many systems /bin is a link to /usr/bin, which is why the same program appears twice.

Using command -v

The command -v form prints a short description and works in scripts on any POSIX shell:

command -v pwd
command -v ls

Sample output:

pwd
/usr/bin/ls

For a built-in or keyword it prints just the name, and for an external program it prints the path. It prints nothing and returns a non-zero status when the command does not exist, so it is a reliable way to check whether a tool is installed:

if command -v curl > /dev/null; then
    echo "curl is installed"
else
    echo "curl is not installed"
fi

The > /dev/null part discards the printed path, because the script only needs the success or failure result.

Listing the Built-ins

The built-in compgen -b prints the name of every built-in command:

compgen -b | wc -l

Output in Bash 5.2:

61

The count differs between Bash versions. To list the keywords, use compgen -k:

compgen -k | head -5

Output:

if
then
else
elif
fi

Getting Help for Each Kind

Built-ins and external commands are documented in different places.

For a built-in, use the help built-in:

help -d cd

Output:

cd - Change the shell working directory.

Running help cd without -d shows the full description, including every option. Built-ins also have a long entry inside the Bash manual page (man bash), but they usually do not have a manual page of their own.

For an external command, use the command's own option or its manual page:

ls --help
man ls

The output of ls --help begins with a usage line, as described in the lesson on command syntax. The command man ls opens the full reference document. Because type tells you which kind of command a name is, it also tells you which help source to try.

Built-ins and External Commands with the Same Name

Some commands exist in both forms, such as echo, printf, pwd, test, and kill. They usually behave in the same way, but small differences exist. The built-in echo is part of Bash, while /usr/bin/echo is a separate program from your system's core utilities, and the options each one accepts may differ between systems. Scripts that need identical behavior everywhere often prefer printf.

You can choose which one runs. Typing the full path forces the external program:

type echo
/usr/bin/echo hello

Sample output:

echo is a shell builtin
hello

The first line shows that plain echo is a built-in. The second command runs the external file directly and prints hello.

Overriding and Bypassing Commands

Because functions come before built-ins in the lookup order, you can replace a built-in with your own function. Define a function named echo that adds a label:

echo() { builtin echo "[wrapped] $*"; }
echo hi

Output:

[wrapped] hi

The function body calls builtin echo, which forces Bash to use the real built-in. Without the builtin word, the function would call itself forever. To skip your own function, use the command word:

command echo hi
builtin echo hi

Output of each:

hi

command echo ignores aliases and functions, and builtin echo runs only the built-in version. Remove the function when you are finished:

unset -f echo
echo hi

Output:

hi

After unset -f echo, the name echo refers to the built-in once more.

Aliases follow the same lookup rule, and type identifies them:

alias ll='ls -l'
type ll
type -t ll

Output:

ll is aliased to `ls -l'
alias

Overriding commands is useful for interactive customization, but be careful when you do it in files that scripts read, because the changed behavior may surprise the next person.

Turning a Built-in Off

The enable built-in can disable a built-in so that Bash falls through to the external version:

enable -n echo
type echo
enable echo
type -t echo

Output:

echo is /usr/bin/echo
builtin

After enable -n echo, the name echo is no longer a built-in, so type reports the file on disk. Running enable echo turns the built-in back on. This is rarely needed in normal work, but it shows that the built-in versus external distinction is a choice Bash makes by lookup, not a property of the name.

Practical Applications

The distinction becomes useful in everyday situations:

  • Choosing a help source. If man cd finds nothing useful, run type cd. It reports a built-in, so help cd is the right place to look.
  • Debugging "wrong version" problems. If a command behaves unexpectedly, run type -a name to see whether an alias, function, or second copy is running instead of the program you expected.
  • Checking dependencies in a script. Using command -v before calling a tool lets a script stop with a clear message instead of failing in the middle of its work.
  • Understanding scope. A subshell or external script cannot change your current directory or variables, because those belong to the parent shell. This is why commands like cd and export must be built in.

0 Comments

Reviewed before they appear

No comments yet.

Bash
Introduction to Bash What Is a Shell? What Is Bash (Bourne Again Shell)? Bash vs. Shell vs. Terminal Bash vs. sh, Zsh, and Fish Where Bash Is Used: Linux, DevOps, and Automation Installing Bash on Linux Using Bash on macOS Using Bash on Windows with WSL Checking Your Bash Version Opening and Using a Terminal Running Your First Bash Command Understanding Command Syntax and Arguments Bash Built-in Commands vs. External Commands Working with pwd, ls, and cd Creating Files and Directories with touch and mkdir Copying, Moving, and Renaming Files Removing Files and Directories Safely Viewing Files with cat, less, head, and tail Searching for Files with find Searching Text with grep Understanding File Paths and Wildcards Using man Pages and Command Help Bash Script Structure Creating Your First Bash Script Understanding the Shebang (#!/usr/bin/env bash) Making a Script Executable with chmod Running a Bash Script in Different Ways Writing Comments in Bash Using echo and printf Reading User Input with read Displaying Helpful Messages in Scripts Using Semicolons and Newlines Bash Variables: Declaration and Usage Rules for Naming Bash Variables Environment Variables Local Variables vs. Environment Variables Exporting Variables with export Common Environment Variables: HOME, PATH, USER, and PWD Read-Only Variables with readonly Removing Variables with unset Command-Line Arguments: $0, $1, and More Special Parameters: $#, $?, $$, $!, $@, and $* Positional Parameters and shift Default Values with Parameter Expansion Single Quotes vs. Double Quotes Escaping Special Characters Command Substitution with $(...) Arithmetic Expansion with $((...)) Brace Expansion Tilde Expansion and Home Directories Filename Expansion and Globbing Understanding Word Splitting Parameter Expansion Basics Why Quoting Variables Matters Standard Input, Standard Output, and Standard Error Redirecting Output with > and >> Redirecting Input with < Redirecting Errors with 2> Combining Standard Output and Standard Error Using Pipes (|) to Connect Commands Using tee to Save and Display Output Here Documents with << Here Strings with <<< Command Lists with ;, &&, and || Conditional Statements with if, then, and fi Using elif and else Testing Conditions with test and [ ] Using [[ ]] for Bash Conditionals Comparing Strings in Bash Checking Whether Files Exist Checking File Types and Permissions Integer Comparisons in Bash Logical Operators: &&, ||, and ! Using case Statements Pattern Matching in case Common Conditional Statement Mistakes for Loops in Bash C-Style for Loops while Loops until Loops Looping Through Files and Directories Looping Through Command Output Safely break and continue Nested Loops select Loops for Simple Menus Avoiding Infinite Loops Defining and Calling Bash Functions Passing Arguments to Functions Returning Status Codes from Functions Understanding return vs. echo in Functions Local Variables in Functions Function Scope and Variable Visibility Organizing Scripts into Functions Sourcing Other Bash Files with source Reusable Bash Utility Functions Indexed Arrays in Bash Creating and Accessing Array Elements Adding and Removing Array Elements Looping Through Arrays Array Length and Indices Associative Arrays in Bash Iterating Over Associative Arrays Passing Arrays to Functions Common Array Pitfalls Processing Text with grep, sed, and awk Using cut, sort, uniq, and wc Finding and Replacing Text with sed Extracting Columns with awk Parsing CSV-Like Text from the Command Line Working with Filenames That Contain Spaces Safe File Handling with find -print0 and xargs -0 Temporary Files with mktemp Reading Files Line by Line Appending Logs to a File Understanding Exit Codes Checking Command Success with $? Using exit to End a Script Fail-Fast Scripts with set -e: Benefits and Caveats Using set -u and set -o pipefail Combining Strict Mode Options Safely Handling Errors with if Statements Traps with trap Cleaning Up Temporary Files on Exit Writing Useful Error Messages Debugging Scripts with bash -x Parsing Script Arguments Validating Required Arguments Handling Optional Arguments Parsing Options with getopts Building Help Messages with --help Setting Defaults for Optional Parameters Handling Unknown Options and Invalid Input Creating Interactive Menus in Bash Foreground and Background Processes Running Commands in the Background with & Managing Jobs with jobs, fg, and bg Waiting for Processes with wait Capturing Process IDs with $! and $$ Sending Signals with kill Handling Interrupts and Termination Signals Running Commands with timeout Basic Process Monitoring from Bash Aliases and Shell Functions: When to Use Each Customizing the Bash Prompt (PS1) Bash Startup Files: .bashrc, .bash_profile, and Profile Interactive vs. Non-Interactive Bash Shell Options with set shopt: Enabling Bash-Specific Options Directory Stack with pushd, popd, and dirs Process Substitution with <(...) and >(...) Advanced Parameter Expansion Pattern Matching and Extended Globbing Bash Script Security Fundamentals Avoiding Command Injection in Shell Scripts Quoting User Input Safely Why eval Is Risky and Safer Alternatives Handling Secrets and Credentials in Scripts Using Least Privilege in Automation Scripts Safe Use of rm and Destructive Commands Validating Paths and User-Supplied Filenames ShellCheck: Static Analysis for Bash Scripts Writing Readable and Maintainable Bash Automating Backups with Bash Creating Log Rotation and Cleanup Scripts Checking Disk Usage and Free Space Monitoring CPU, Memory, and System Load Checking Website Availability with curl Automating Updates on Ubuntu and Debian Scheduling Bash Scripts with cron Using Lock Files to Prevent Duplicate Runs Building a Simple Health-Check Script Sending Notifications from Bash Scripts Using Bash in Git Workflows Bash Scripts for Docker Automation Automating Docker Container Health Checks Using Bash in CI/CD Pipelines Environment Configuration for Deployment Scripts Automating SSH Tasks with Bash Deploying a Web Application with a Bash Script Using Bash to Process Application Logs Building Repeatable Development Setup Scripts Bash Debugging Techniques Using set -x and PS4 for Debug Traces Testing Bash Scripts with Bats Mocking Commands During Script Tests Testing Error Paths and Exit Codes Writing Portable Shell Scripts Bash-Specific Syntax vs. POSIX Shell Syntax Using ShellCheck in a Development Workflow Performance Tips for Bash Scripts Build an Interactive Calculator Build a File and Directory Organizer Build a Timestamped Backup Script Build a Log File Analyzer Build a Bulk File Renaming Tool Build a System Resource Monitoring Script Build a Website Uptime Checker Build a User Account Audit Report Build a Database Backup Automation Script Build a Docker Cleanup Helper Build a Server Health-Check and Alert Script Build a Deployment Automation Script Build a Menu-Driven Linux Administration Toolkit Build a Safe Automated Backup and Restore Workflow
Ask about this post
AI Ask about this post

Ask questions about Bash Built-in Commands vs. External Commands and get answers drawn from it.

Signed-in readers only.