← Work

C (GNU89) / POSIX system calls / Linux (Ubuntu 20.04 LTS)

Simple Shell

A Unix shell written from scratch in C: PATH resolution, seven builtins, command chaining with &&, || and ;, aliases, comments and variable replacement, built on POSIX system calls for Ubuntu 20.04.

  • C (GNU89)
  • POSIX system calls
  • Linux (Ubuntu 20.04 LTS)
  • gcc toolchain
Role
Author
Year
2023
builtins
7

Trace samples for this project are illustrative

A terminal session in Simple Shell showing command chaining, an alias, setenv, variable replacement and comments

A lightweight Unix-like command interpreter implemented in C, designed to mirror the essential behavior of early Unix shells while remaining intentionally minimal. The project focuses on core process control, parsing, and environment management, providing a clean foundation for understanding how shells work under the hood.


Overview

Simple Shell reads user input, tokenizes commands, resolves executables using the PATH, and executes programs in child processes. It supports common shell behaviors such as chaining, comments, and aliases, alongside a practical set of built-in commands for environment and session control.


Key Capabilities

  • Command execution: runs external programs via fork() + execve()
  • PATH resolution: locates executables using the PATH environment variable
  • Builtins: cd, exit, env, setenv, unsetenv, help, history
  • Chaining: supports ;, &&, and ||
  • Comments: ignores content after #
  • Aliases: define and expand custom command aliases
  • Variable replacement: expands environment variables in input

Architecture Highlights

  • Parser and tokenizer: handle command splitting and argument extraction
  • Execution engine: dispatches builtins or forks external commands
  • Environment layer: maintains a linked-list representation of environ
  • History manager: persists and reloads command history

Usage

Compile

shell
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o simple_shell

Run (interactive)

shell
./simple_shell

Run (non-interactive)

shell
echo "ls -l; pwd" | ./simple_shell

Example Session

shell
$ ./simple_shell
simple_shell$ ls
file1.c  file2.c  README.md
simple_shell$ cd ..
simple_shell$ pwd
/home/user/projects
simple_shell$ setenv MYVAR Hello
simple_shell$ echo $MYVAR
Hello
simple_shell$ exit

Requirements

  • Ubuntu 20.04 LTS (or compatible Linux)
  • GNU Compiler Collection (gcc)
  • Standard POSIX system calls

Design Goals

  • Minimal and readable codebase for learning purposes
  • Accurate core shell behavior without unnecessary complexity
  • Portable and standards-based (GNU89 + POSIX)

License

Open source; free to use and modify.