Skip to content

About

A compact Unix shell written in C as part of the 42 curriculum.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

Minishell

A compact Unix shell written in C as part of the 42 curriculum.

The project recreates a practical subset of Bash and focuses on process creation, file descriptors, pipes, signals, parsing, environment management, and command execution.

Overview

Minishell reads a command line, converts it into an internal command representation, expands variables, applies redirections, builds pipelines, and executes built-in or external commands.

The implementation is divided into several stages:

input -> lexer -> parser -> expansion -> redirections -> execution

Features

  • Interactive prompt powered by GNU Readline
  • Command history
  • Execution of programs through absolute, relative, or PATH-resolved paths
  • Pipelines using pipe, fork, dup2, and execve
  • Input and output redirections:
    • < input redirection
    • > output redirection
    • >> append redirection
    • << heredoc
  • Environment-variable expansion, including $?
  • Single-quote and double-quote handling
  • Bash-like handling of Ctrl-C, Ctrl-D, and Ctrl-\
  • Exit-status propagation
  • Built-in commands:
    • echo with -n
    • cd
    • pwd
    • export
    • unset
    • env
    • exit

Architecture

Input and validation

The interactive loop reads user input with readline, stores non-empty commands in history, and rejects unsupported or incomplete syntax before execution.

Lexer

The lexer scans the input while preserving quoted sections and identifies operators such as pipes and redirections.

Parser

Tokens are converted into command structures containing:

  • executable name
  • argument vector
  • redirections
  • pipeline position
  • heredoc information

Expansion

The expansion stage resolves environment variables and the previous command's exit status. Expansion behavior depends on quote context.

Executor

The executor prepares heredocs and redirections, creates pipelines, forks child processes, resolves executable paths, and collects exit statuses.

Built-ins that must modify shell state, such as cd, export, and unset, are executed in the parent process when they are not part of a pipeline.

Signal handling

Signal behavior changes depending on whether the shell is waiting for input, executing a child process, or processing a heredoc.

Build

GNU Readline must be installed.

On Debian or Ubuntu:

sudo apt install libreadline-dev

On macOS with Homebrew:

brew install readline

Compile the project:

make

Clean object files:

make clean

Remove all generated files:

make fclean

Rebuild:

make re

Usage

Start the shell:

./minishell

Example commands:

echo "Hello, $USER"
pwd
cd ..
ls -la | grep minishell
cat < input.txt | wc -l > result.txt
echo "$?"

Exit with:

exit

or press Ctrl-D on an empty prompt.

Technical concepts practiced

  • Unix process lifecycle
  • Parent and child process coordination
  • File-descriptor management
  • Inter-process communication
  • Signal handling
  • Lexical analysis and parsing
  • Dynamic-memory ownership
  • Environment-variable management
  • Error handling and exit codes

Limitations

This project implements the behavior required by the Minishell assignment and is not intended to be a complete Bash replacement.

Features such as logical operators, command grouping, wildcard expansion, job control, and scripting may be outside the implemented scope unless added separately.

Testing

Useful test categories include:

  • simple and invalid commands
  • quoted and unquoted arguments
  • environment expansion
  • chained pipelines
  • all redirection types
  • permission and missing-file errors
  • built-ins inside and outside pipelines
  • signal behavior
  • exit-status comparison with Bash
  • memory-leak checks

For behavioral verification, commands can be run in both Bash and Minishell and their output and exit status compared.

Author note

This repository is presented as a systems-programming project demonstrating practical knowledge of C, Unix processes, pipes, signals, parsing, and shell execution.

About

A compact Unix shell written in C as part of the 42 curriculum.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages