Minishell
A minimal implementation of a Bash-like shell in C, featuring built-in commands, redirections, environment variables, and process management.
2022-02-01
low-levelOpen GitHub

Minishell
A bash-like shell written in C from scratch. It parses input into a command structure, handles variable expansion and quoting, forks child processes, wires up pipes, and deals with redirections the same way bash does.
Parsing
- Input is tokenized into words, operators, and redirections
- Single quotes block all expansion inside them
- Double quotes allow variable expansion inside
$VARIABLEand$?(last exit code) are both expanded- Syntax errors (unclosed quotes, unexpected tokens) are caught and reported
- Heredoc (
<<) reads until the delimiter line
Execution
- Pipelines of any length work with
| - Redirections:
<for input,>for output,>>to append - Commands are resolved from
$PATH - Signals are handled non-blocking:
Ctrl+C,Ctrl+D,Ctrl+\
Built-ins
| Command | Behavior |
|---|---|
| echo [-n] | Print to stdout, -n skips the newline |
| cd [path] | Change directory, updates PWD and OLDPWD |
| pwd | Print current directory |
| export [name=value] | Set or declare environment variables |
| unset [name] | Remove an environment variable |
| env | Print all exported variables |
| exit [n] | Exit with optional status code |
Signal behavior
| Signal | When idle | During a command |
|---|---|---|
| Ctrl+C | New prompt | Kill the running process |
| Ctrl+D | Exit shell | |
| Ctrl+\ | Nothing | Quit the process |
Examples
minishell$ echo "Hello, $USER"
minishell$ ls -la | grep ".c" | wc -l
minishell$ cat < input.txt | grep pattern > output.txt
minishell$ cat << EOF
> line one
> line two
> EOFSCREENSHOTS // 01 FRAMES

01 / 01