Bash Built-in Commands vs. External Commands
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, andtype. - An external command is a separate executable file stored on disk, such as
/usr/bin/lsor/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, andcase. - 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:
- An alias with that name.
- A shell keyword, such as
if. - A function defined in the current shell.
- A built-in command.
- An external program found by searching the directories listed in the
PATHvariable, 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 cdfinds nothing useful, runtype cd. It reports a built-in, sohelp cdis the right place to look. - Debugging "wrong version" problems. If a command behaves unexpectedly, run
type -a nameto see whether an alias, function, or second copy is running instead of the program you expected. - Checking dependencies in a script. Using
command -vbefore 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
cdandexportmust be built in.