Skip to content

aboubakrbkd/Minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

260 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Minishell

A minimal Unix shell implementation written in C, featuring command execution, pipes, redirections, and built-in commands.

Overview

Minishell is a lightweight shell that mimics the behavior of /bin/bash. It provides essential shell functionality including command parsing, execution, environment management, and I/O redirection.

Features

Core Functionality

  • Command Execution: Execute external programs with proper PATH resolution
  • Pipes (|): Chain multiple commands together
  • Input/Output Redirections:
    • Input redirection (<)
    • Output redirection (>)
    • Append redirection (>>)
    • Here-documents (<<)
  • Environment Variable Expansion: Support for $VARIABLE and $? (exit status)
  • Quote Handling: Proper parsing of single and double quotes
  • Signal Handling: Handles SIGINT (Ctrl+C) and SIGQUIT (Ctrl+)

Built-in Commands

  • echo: Print text with optional -n flag
  • cd: Change directory with proper PATH handling
  • pwd: Print working directory
  • export: Set environment variables
  • unset: Remove environment variables
  • env: Display environment variables
  • exit: Exit the shell with optional exit code

Requirements

  • GCC or Clang compiler
  • readline library: For command-line editing and history
  • Make: For building the project
  • Unix-like system: Linux, macOS, or WSL

Installation

macOS (with Homebrew)

brew install readline

Linux (Debian/Ubuntu)

sudo apt-get install libreadline-dev build-essential

Building

make              # Compile the shell
make clean        # Remove object files
make fclean       # Remove all compiled files and executable

Usage

Starting the Shell

./minishell

Example Commands

# Execute external commands
ls -la
pwd
echo "Hello, World!"

# Use pipes
cat file.txt | grep "pattern" | wc -l

# Redirections
echo "Hello" > output.txt
cat < input.txt > output.txt
echo "Appending" >> file.txt

# Here-documents
cat << EOF
This is a here-document
EOF

# Environment variables
export MY_VAR="value"
echo $MY_VAR
unset MY_VAR

# Change directory
cd /path/to/directory
cd ..
cd ~

# Exit code
echo $?

Built-in Commands Reference

echo

Print text to standard output.

echo "Hello"          # Print text
echo -n "No newline"  # Without trailing newline

cd

Change the working directory.

cd /home/user
cd ..
cd ~
cd -

pwd

Print the current working directory.

pwd

export

Set environment variables.

export VAR_NAME="value"
export PATH=$PATH:/new/path

unset

Remove environment variables.

unset VAR_NAME

env

Display all environment variables.

env

exit

Exit the shell.

exit
exit 42

clear

Clear the terminal screen.

clear

Implementation Details

Parsing

The shell parses user input into tokens and constructs a command structure that supports:

  • Simple commands
  • Pipes connecting multiple commands
  • Input/output redirections
  • Command chaining

Expansion

Variable expansion is performed at parse time:

  • $VARIABLE: Expands to environment variable value
  • $?: Expands to the exit status of the last command
  • Quote handling prevents expansion within single quotes

Execution

Commands are executed in child processes using fork() and execve(), maintaining the parent shell process for subsequent commands.

Architecture

User Input
    ↓
Tokenization & Parsing
    ↓
Variable Expansion
    ↓
Quote Removal
    ↓
Redirection Setup
    ↓
Command Execution
    ↓
Output

Limitations

  • No job control (background processes are not fully supported)
  • Limited wildcard expansion
  • No command history search
  • No aliases
  • Simplified regex handling

Exit Codes

The shell returns the exit code of the last executed command:

  • 0: Success
  • 1-127: Command-specific error
  • 128+N: Command terminated by signal N
  • 127: Command not found
  • 126: Command not executable

Author

Aboubakr - GitHub Mouad - GitHub

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •