That's an excellent question that gets right to the heart of why builtins exist and are so fundamental to the shell!
Shell builtins can directly affect the shell environment in ways that external commands (executables found in $PATH) simply cannot, or can only do indirectly. This direct interaction is their biggest advantage and why they are implemented as builtins.
Here's how they do it:
-
Changing the Current Working Directory (
cd):- How
cdaffects the environment: Thecdcommand fundamentally changes the shell's current working directory. All subsequent commands you run (unless specified otherwise) will operate relative to this new directory. - Why it must be a builtin: If
cdwere an external program, it would change its own working directory when executed. However, once that externalcdprogram finished, it would exit, and the shell's original working directory would remain unchanged. An external program cannot modify the environment (like the current directory variable) of its parent process (the shell). - Direct effect:
cddirectly modifies the shell's internal variable that tracks the current directory, making the change immediate and permanent for the current shell session.
- How
-
Setting Environment Variables (
export,declare,unset):- How they affect the environment: Commands like
exportintroduce or modify environment variables that are then inherited by all child processes (other commands, scripts, etc.) launched from that shell.unsetremoves them.declaresets shell variables or attributes. - Why they must be builtins: Similar to
cd, an external program cannot directly modify the environment variables of its parent shell. When an external programexport_varruns, it can create or modify variables within its own process, but these changes are lost as soon asexport_varexits. - Direct effect: These builtins modify the shell's internal table of variables and their values, making them visible and available to the shell and its children.
- How they affect the environment: Commands like
-
Managing Aliases (
alias,unalias):- How they affect the environment:
aliasdefines shortcuts for commands, andunaliasremoves them. These definitions change how the shell interprets commands you type. - Why they must be builtins: Aliases are a feature of the shell's command parsing. Only the shell itself can understand and apply alias definitions before trying to find an executable. An external program couldn't "tell" the shell to use an alias.
- Direct effect: They modify the shell's internal list of alias definitions, directly influencing command lookup.
- How they affect the environment:
-
Controlling Shell Options (
set,shopt):- How they affect the environment: Commands like
set(e.g.,set -eto exit on error) andshopt(e.g.,shopt -s autocdfor automatic directory changing) control the behavior and features of the shell itself. - Why they must be builtins: These are internal configuration settings. No external program could possibly reconfigure the shell's core operational logic.
- Direct effect: They directly toggle internal flags and settings within the shell process, altering its behavior.
- How they affect the environment: Commands like
-
Managing Functions (
function,unset -f):- How they affect the environment: Shell functions are reusable blocks of shell script that run within the current shell process. You define them using
functionsyntax. - Why they must be builtins: Functions are part of the shell's scripting capabilities and are interpreted directly by the shell.
- Direct effect: They add or remove executable code blocks directly into the shell's memory for execution.
- How they affect the environment: Shell functions are reusable blocks of shell script that run within the current shell process. You define them using
In essence, builtins are the shell's internal toolkit for managing its own state and behavior. External commands, on the other hand, are separate programs that interact with the shell (often by being launched by it and sometimes by inheriting its environment), but they cannot reach in and modify the shell's core processes, variables, or definitions. This clear separation of responsibilities makes the shell both powerful and stable.