Overview
Built-in commands are:- echo: Display a line of text
- cd: Change the current directory
- pwd: Print working directory
- export: Set environment variables
- unset: Remove environment variables
- env: Display environment variables
- exit: Exit the shell
Built-in commands are executed within the shell process itself, making them faster than external commands and allowing them to modify the shell’s environment.
Command Reference
echo - Display text
echo - Display text
cd - Change directory
cd - Change directory
Syntax
Description
Changes the current working directory. If no directory is specified, changes to the home directory.Arguments
DIRECTORY: Path to change to (relative or absolute)- No argument: Changes to
$HOME
Examples
Error Handling
Successful cd commands set the exit status to 0, while errors set it to 1.
pwd - Print working directory
pwd - Print working directory
export - Set environment variables
export - Set environment variables
Syntax
Description
Sets or displays environment variables. Without arguments, displays all variables withdeclare -x prefix.Usage Patterns
Display All Variables
Set Variable with Value
Set Variable without Value
Multiple Variables
Variable Naming Rules
Examples
The export command modifies the shell’s environment list, which is inherited by child processes (see init_shell.c:35-62).
unset - Remove environment variables
unset - Remove environment variables
Syntax
Description
Removes environment variables from the shell’s environment.Examples
Behavior
- Removing a non-existent variable is not an error (no output)
- Multiple variables can be removed in one command
- Exit status is always 0
The unset command searches the environment list and removes matching entries (see builtins.c:15-53).
env - Display environment variables
env - Display environment variables
Syntax
Description
Displays all environment variables inNAME=VALUE format.Examples
Usage with Other Commands
Error Handling
Unlike bash’s env, Minishell’s env command only displays variables and doesn’t support running commands with modified environments.
The env command iterates through the environment list and prints each entry (see init_shell.c:68-76).
exit - Exit the shell
exit - Exit the shell
Syntax
Description
Exits the shell with an optional exit status code.Arguments
N: Exit status (integer between 0-255)- No argument: Exits with status of last command ($?)
Examples
Exit Status Codes
0: Success1: General error2: Misuse of shell builtin126: Command cannot execute127: Command not found130: Terminated by Ctrl+C- Custom: Any value specified by user
Error Handling
When exiting, Minishell displays a red “Exit” message before terminating (see exit.c:63-85).
Exit Status Codes
All built-in commands set the global exit status ($?):
Built-ins vs External Commands
Built-in commands are executed within the shell process, while external commands (like
ls, cat, grep) are executed in child processes. This means:- Built-ins can modify the shell’s environment (like
cdandexport) - Built-ins are faster (no process creation overhead)
- Built-ins always execute even if an external command exists with the same name
Implementation Details
For developers interested in the internals:- Built-in detection: minishell.h:150 (
is_builtin()) - Built-in execution: init_shell.c:118-132 (
execute_builtin()) - Individual implementations: builtins.c