Skip to main content
Minishell implements 7 built-in commands that are executed directly by the shell without creating a new process.

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

Syntax

echo [OPTIONS] [ARGUMENTS...]

Description

Displays text to standard output. Supports the -n option to suppress the trailing newline.

Options

  • -n: Do not output the trailing newline

Examples

# Basic usage
echo Hello World
# Output: Hello World

# Suppress newline
echo -n "No newline"
# Output: No newline (without newline)

# Multiple -n flags (all are processed)
echo -n -n -n "Still no newline"
# Output: Still no newline

# Variable expansion
echo "Current user: $USER"
# Output: Current user: john

# Quotes preserved
echo 'Single quotes: $USER'
# Output: Single quotes: $USER

Implementation Notes

The echo command processes all consecutive -n flags before printing output (see builtins.c:87-105).

Syntax

cd [DIRECTORY]

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

# Change to home directory
cd
# Changes to $HOME

# Absolute path
cd /usr/local/bin

# Relative path
cd ../documents

# Change to home explicitly
cd ~
cd $HOME

Error Handling

# Directory doesn't exist
cd /nonexistent
# Output: cd: /nonexistent: No such file or directory
# Exit status: 1

# Permission denied
cd /root
# Output: cd: /root: Permission denied
# Exit status: 1
Successful cd commands set the exit status to 0, while errors set it to 1.

Syntax

pwd

Description

Prints the absolute path of the current working directory.

Examples

pwd
# Output: /home/user/projects/minishell

cd /tmp
pwd
# Output: /tmp

Error Handling

# Too many arguments
pwd extra arguments
# Output: pwd: too many arguments
The pwd command uses getcwd() to retrieve the current directory path (see builtins.c:55-68).

Syntax

export [NAME[=VALUE]...]

Description

Sets or displays environment variables. Without arguments, displays all variables with declare -x prefix.

Usage Patterns

Display All Variables

export
# Output:
# declare -x HOME=/home/user
# declare -x PATH=/usr/bin:/bin
# declare -x USER=john

Set Variable with Value

# Set new variable
export MY_VAR=hello
echo $MY_VAR
# Output: hello

# Update existing variable
export PATH=/new/path:$PATH

Set Variable without Value

# Creates variable with empty value
export EMPTY_VAR
echo "$EMPTY_VAR"
# Output: (empty string)

Multiple Variables

export VAR1=value1 VAR2=value2 VAR3=value3

Variable Naming Rules

Variable names must contain only alphabetic characters. Invalid names result in an error:
export 123VAR=value
# Output: not valid in this context:
# Exit status: 1

export VAR-NAME=value
# Output: not valid in this context:
# Exit status: 1

export =value
# Output: not valid in this context:
# Exit status: 1

Examples

# Configure environment
export EDITOR=vim
export LANG=en_US.UTF-8
export DEBUG=1

# Modify PATH
export PATH=$HOME/bin:$PATH

# Set multiple related variables
export PROJECT_NAME=minishell
export PROJECT_PATH=/home/user/projects/minishell
The export command modifies the shell’s environment list, which is inherited by child processes (see init_shell.c:35-62).

Syntax

unset [NAME...]

Description

Removes environment variables from the shell’s environment.

Examples

# Remove single variable
export MY_VAR=test
echo $MY_VAR
# Output: test

unset MY_VAR
echo $MY_VAR
# Output: (empty - variable removed)

# Remove multiple variables
export VAR1=a VAR2=b VAR3=c
unset VAR1 VAR2 VAR3

# Unset PATH (not recommended)
unset PATH
# Commands may not work after this!

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
Be careful when unsetting critical variables like PATH, HOME, or USER. This may cause commands to fail or behave unexpectedly.
The unset command searches the environment list and removes matching entries (see builtins.c:15-53).

Syntax

env

Description

Displays all environment variables in NAME=VALUE format.

Examples

env
# Output:
# HOME=/home/user
# PATH=/usr/bin:/bin:/usr/local/bin
# USER=john
# SHELL=/bin/bash
# LANG=en_US.UTF-8
# ...

Usage with Other Commands

# Count environment variables
env | wc -l

# Find specific variable
env | grep PATH

# Check if variable exists
env | grep "^MY_VAR="

Error Handling

# env does not accept arguments
env --help
# Output: env: '--help': not accepted
# Exit status: 127

env extra_arg
# Output: env: 'extra_arg': not accepted
# Exit status: 127
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).

Syntax

exit [N]

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 with success
exit 0

# Exit with error
exit 1

# Exit with custom code
exit 42

# Exit with last command status
ls /nonexistent
exit
# Exits with status 1

Exit Status Codes

  • 0: Success
  • 1: General error
  • 2: Misuse of shell builtin
  • 126: Command cannot execute
  • 127: Command not found
  • 130: Terminated by Ctrl+C
  • Custom: Any value specified by user

Error Handling

# Non-numeric argument
exit hello
# Output: exit: hello: numeric argument required
# Exits with status: 2

# Negative numbers allowed
exit -1
# Exits with status: 255 (wraps around)

# Too many arguments
exit 0 1 2
# Does not exit (stays in shell)
The exit command only accepts numeric arguments. Non-numeric values cause the shell to exit with status 2.
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 ($?):
# Success
echo "Hello"
echo $?
# Output: 0

# Command error
cd /nonexistent
echo $?
# Output: 1

# Command not found
nonexistent_command
echo $?
# Output: 127

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 cd and export)
  • 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

Build docs developers (and LLMs) love