Unix & Systems Programming

Minishell: Bash-Like Unix Shell in C

A Bash-like Unix shell in C with pipes, redirections, here-docs, variable expansion, built-ins, && and || operators, subshells, and wildcards.

Last updated:

Minishell: Bash-Like Unix Shell in C

Introduction

Minishell is a Unix shell written in C by two students at 42 and modeled on Bash. It offers an interactive prompt with history through GNU Readline, runs programs found through PATH, and supports quoting, environment variables, the last exit status, redirections, here-docs, pipelines, and signals.

The bonus version adds && and || with parentheses for grouping, subshells, and * wildcard expansion in the current directory, alongside seven built-in commands: echo, cd, pwd, export, unset, env, and exit.

The problem

A shell looks simple until you write one. A single line can mix quotes, variables, redirections, here-docs, pipes, and logical operators, and each part has precise rules about when it is expanded, how it is split, and which process runs it. Any difference from Bash is immediately visible to the user.

It also has to be robust: report syntax errors without crashing, never leak file descriptors or memory across thousands of commands, and react to Ctrl+C, Ctrl+D, and Ctrl+\ the way Bash does, both at the prompt and while a command is running.

Architecture

Each line goes through a series of stages. A tokenizer splits the input while respecting quotes and operators; an analyzer checks the tokens for syntax errors such as misplaced separators or a missing here-doc delimiter; and the parser builds a list of commands with their arguments and redirections. Here-docs are then read before anything runs.

Execution expands variables, removes quotes, and expands wildcards in arguments and redirections, resolves programs through PATH, connects commands with pipes, applies redirections, and runs built-ins in the shell process when they must change its state. In the bonus, && and || decide what runs next from the exit status, and parenthesized groups run in a subshell.

Implementation decisions

Validate before executing
A dedicated analyzer rejects malformed input before any process is created, so a syntax error never leaves half a pipeline running and the exit status matches Bash.
Here-docs read up front
All here-docs are collected before execution starts, with variable expansion unless the delimiter is quoted, so a pipeline never stops halfway through to wait for here-doc input.
Expansion as its own phase
Variables, quotes, and wildcards are expanded after parsing, separately for arguments and for redirection targets, which reproduces Bash cases such as quoted variables and ambiguous redirects.
Built-ins in the right process
Built-ins that change the shell's state, like cd, export, unset, and exit, run in the shell itself when they stand alone and in a child process inside a pipeline, as in Bash.

Technical challenges

  • Tokenizing input with nested single and double quotes, operators, and parentheses without changing its meaning
  • Matching Bash's expansion rules for $VAR, $?, quotes, and wildcards in both arguments and redirections
  • Closing every unused pipe end and restoring standard input and output so that no file descriptor leaks between commands
  • Handling signals differently at the prompt, during here-doc input, and while child processes are running

Results

  • Runs pipelines, redirections, here-docs, and built-ins with Bash-compatible exit statuses
  • Supports &&, ||, parentheses, subshells, and wildcards in the bonus version
  • Reports syntax errors cleanly instead of crashing or running partial commands
  • An interactive prompt with command history through GNU Readline

Related case studies