Skip to content

πŸŒ€A Unix shell implemented in C with full support for pipes, redirections, built-ins, and environment variable expansion

Notifications You must be signed in to change notification settings

mdbentaleb/Minishell_42

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐚 Minishell

Image

πŸ“Œ Overview

Minishell is a core project from the 42 curriculum that replicates the behavior of a simplified Unix shell. It aims to deepen your understanding of process management, file descriptors, signals, parsing, and job control by building your own command-line interpreter from scratch in C.

✨ Features

  • 🧠 Custom shell parsing and syntax handling
  • πŸ“œ Support for sequential and piped command execution
  • πŸͺ„ Built-in commands: cd, echo, env, exit, export, pwd, unset, history
  • 🌍 Environment variable management and expansion ($VAR support)
  • 🧱 Redirections: input <, output >, append >>, and heredocs <<
  • πŸ”— Full support for pipelines using |
  • 🧼 Proper memory and file descriptor management
  • πŸ“Ά Signal handling: handles EOF (ctl+D), SIGINT (Ctrl+C) and SIGQUIT (Ctrl+\) gracefully
  • 🧡 Forking and process execution with correct exit codes
  • πŸ§ͺ Error handling for syntax errors and "command not found"
  • πŸ”„ Command history persistence across sessions
  • 🧠 Advanced quoting, escaping, and syntax error detection
  • πŸͺ„ Auto-completion support (via readline hooks)
  • πŸ“œ Custom shell prompt

🧭 What Minishell Supports

🧡 Execution Engine

  • Parse and execute commands using fork() and execve()
  • Wait for child processes using wait() / waitpid() and return correct exit statuses
  • Execute piped commands by managing file descriptors correctly

🧹 Parsing & Tokenization

  • Split and analyze user input: detect pipes, redirections, quotes, and variable expansions
  • Respect shell grammar: handle nested quotes (', ") and escape characters
  • Handle empty inputs and multiple spaces or tabs gracefully

βš–οΈ Built-in Commands (No execve)

  • echo [-n] β€” Print arguments
  • cd [dir] β€” Change current directory
  • pwd β€” Print working directory
  • export VAR=val β€” Set environment variable
  • unset VAR β€” Remove variable
  • env β€” List environment variables
  • history β€” Show command history for the current session
  • exit [code] β€” Exit the shell with an optional status code

πŸ“‚ Redirections

  • > β€” Redirect output (overwrite)
  • >> β€” Redirect output (append)
  • < β€” Redirect input
  • << β€” Heredoc input until limiter is found

πŸ’¬ Environment Variables

  • Handle $VAR expansion from the current environment and local variables
  • Expand variables inside double quotes ", but not inside single quotes '

⚠️ Signal Handling

  • Ctrl+C (SIGINT) β€” Interrupts current input line without exiting
  • Ctrl+\ (SIGQUIT) β€” Ignored in the main shell, passed to child processes like in Bash
  • Ctrl+D (EOF) β€” Exits the shell if the input line is empty (end-of-file condition)

πŸ› οΈ Technical Constraints

  • ❌ No use of system(), printf(), or external parsing libraries

  • βœ… Must use readline for line input and command history

  • πŸ“ No memory or file descriptor leaks (must pass valgrind or leaks)

  • πŸ”€ Code must comply with 42 Norminette:

    • Maximum 25 lines per function
    • Maximum 4 variables per function
    • Maximum 4 arguments per function

▢️ How to Use

πŸ› οΈ Compilation

To compile the project, simply run:

make

This will generate the minishell executable.

πŸš€ Running Minishell

Start your shell with:

./minishell

You'll be greeted with a custom prompt where you can type commands just like in Bash.

πŸ’‘ Tips

  • Use up/down arrows to navigate command history
  • Chain commands using pipes (|)
  • Redirect output and input using >, >>, <, and <<
  • Use Ctrl+C to cancel a running foreground command
  • Type exit to quit the shell

πŸ§ͺ Example Usage

zmbash-2.5$ echo hello | cat -e
hellozmbash-2.5$

zmbash-2.5$ export NAME=world
zmbash-2.5$ echo $NAME
world
zmbash-2.5$

zmbash-2.5$ ls > out.txt
zmbash-2.5$ cat < out.txt | grep .c
main.c
utils.c
zmbash-2.5$

About

πŸŒ€A Unix shell implemented in C with full support for pipes, redirections, built-ins, and environment variable expansion

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published