Learn what builtin commands are and how they differ from executables.
Commands like cd and ls may look similar, but they work in different ways internally.
Some commands are builtin commands that the shell handles itself, while others are executables stored as files on your system.
In this concept, we'll learn the difference between builtin commands and executables, and how to identify them using the type command.
Commands like cd and ls may look similar, but they work in different ways internally.
Some commands are builtin commands that the shell handles itself, while others are executables stored as files on your system.
In this concept, we'll learn the difference between builtin commands and executables, and how to identify them using the type command.
A builtin is a command that is part of the shell itself. The code for these commands is built directly into the shell's binary. Because they are part of the shell, they execute very quickly and do not need to spawn a new process.
Executables are binary files stored on your computer's hard drive, usually in directories like /bin or /usr/bin.
When you run an executable (like ls, grep, and cat), the shell searches through a list of directories to find the file. Once found, it executes the file as a new process.
Here are some commonly used builtin commands in Bash:
cd - Changes the shell's current directoryecho - Prints text to the terminalpwd - Prints the current working directorySome commands are builtins because they modify the shell's internal state. For example, if cd were an external program, it would only change its own directory and then exit, so your shell’s directory would stay the same.
Other commands like echo and pwd are implemented as builtins because they are used frequently and must run efficiently. By running directly inside the shell, they avoid the overhead of starting a new process each time.
type commandThe type command can be used to determine whether a command is a builtin or an executable:
# Switch to Bash shell if needed
$ bash
# Check if a command is a builtin
$ type cd
cd is a shell builtin
If the command is an executable, type prints the path to where the file is located:
$ type ls
ls is /bin/ls
You've now learned about builtin commands and how they differ from executables.
A quick recap of what we've covered:
For more details on builtin commands, check out the Bash manual.