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

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
  • $VARIABLE and $? (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

CommandBehavior
echo [-n]Print to stdout, -n skips the newline
cd [path]Change directory, updates PWD and OLDPWD
pwdPrint current directory
export [name=value]Set or declare environment variables
unset [name]Remove an environment variable
envPrint all exported variables
exit [n]Exit with optional status code

Signal behavior

SignalWhen idleDuring a command
Ctrl+CNew promptKill the running process
Ctrl+DExit shell
Ctrl+\NothingQuit 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
> EOF

SCREENSHOTS // 01 FRAMES

Screenshot 1
01 / 01