1. Work
  2. Mini Shell in C
  3. Docs

Mini Shell in C

POSIX command line shell in C with fork/execvp process control, built-ins and signal handling.

2 min read

Source on GitHub

A lightweight POSIX-compliant Unix command line interpreter written in C. Implements interactive command parsing, process lifecycle management via fork() and execvp(), built-in command dispatch, and signal handling.

Overview

Command interpreters serve as the primary userspace interface to the operating system kernel. Mini Shell demonstrates how modern Unix shells parse user intent, manage parent-child process relationships, configure process environments, and handle signals from the terminal.

Key Features

  • Interactive REPL: Displays an interactive prompt (myshell> ), processes multiline or spaced tokens, and cleans trailing delimiters.
  • Process Lifecycle Control: Clones the shell process using fork(), dispatches external binaries through execvp(), and awaits child termination with waitpid().
  • Built-in Command Handling: Executes process-internal directives that cannot run in child forks:
    • cd <path>: Changes the current working directory via chdir().
    • pwd: Prints the current working directory using getcwd().
    • help: Displays built-in manual and supported commands.
    • exit: Terminates the shell process cleanly.
  • Signal Interception: Catches terminal interrupts (SIGINT from Ctrl+C and SIGTSTP from Ctrl+Z) so that running foreground children terminate without killing the parent shell.
  • Error Handling: Uses perror() and standardized POSIX status code returns.

Project Structure

mini shell/
├── main.c              # Core shell entry point, parser, and execution logic
├── SPECIFICATION.md    # Detailed functional specification and test checklist
└── README.md           # Documentation and operational manual

Requirements

  • GCC, Clang, or any standard C compiler
  • POSIX-compliant operating system (macOS, Linux, BSD)

Compilation

Compile using standard flags:

gcc -Wall -Wextra -O2 main.c -o myshell

On macOS:

clang -Wall -Wextra -O2 main.c -o myshell

Usage

Start the interactive shell:

./myshell

Example Session

myshell> pwd
~/mini-shell
myshell> ls -la
total 168
drwxr-xr-x  5 user  staff    160 Sep 14 16:15 .
-rw-r--r--  1 user  staff   4321 Sep 14 16:15 SPECIFICATION.md
-rw-r--r--  1 user  staff   2890 Sep 14 16:16 README.md
-rw-r--r--  1 user  staff   5820 Mar  8 2026 main.c
-rwxr-xr-x  1 user  staff  34120 Sep 14 16:15 myshell
myshell> cd ..
myshell> pwd
~
myshell> help
Mini Shell built-in commands:
  cd <dir>  - Change directory
  pwd       - Print working directory
  help      - Show help menu
  exit      - Exit the shell
myshell> exit

Project Status

Core feature set complete. Verified process execution and built-in dispatch across macOS and Linux environments.

Authors and Acknowledgment

Developed by Adam Anwar for operating systems coursework and systems programming study.

License

MIT License.

Support

Submit issues, questions, or bug reports to the GitHub issue tracker: https://github.com/ADM1SH/mini-shell/issues

Roadmap

  • Done: Core architecture and baseline implementation.
  • Done: Functional verification and test coverage.
  • To do: Add history search with up/down arrow keys
  • To do: Implement job control (bg, fg, CTRL+Z)

Contributing

Contributions are welcome.

  1. Fork the repository.
  2. Create a feature branch: git checkout -b feature/improvement.
  3. Commit your changes: git commit -m "feat: enhance functionality".
  4. Push to the branch: git push origin feature/improvement.
  5. Open a Pull Request.

Back to top