Skip to content

best-collaborators/minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Minishell 🐚

42 School C Norminette

A minimal Unix shell implementation created as part of the 42 school curriculum. This project recreates a simplified version of bash, implementing core shell functionalities including command execution, piping, redirections, and built-in commands.

πŸ“‹ Table of Contents

🎯 About

Minishell is a 42 school project that challenges students to create their own shell. The goal is to understand how a shell works by implementing one from scratch, dealing with processes, file descriptors, signals, and memory management while adhering to strict coding standards (Norminette).

This project provides hands-on experience with:

  • Process creation and management
  • Inter-process communication (pipes)
  • File descriptor manipulation
  • Signal handling
  • Memory management
  • Parsing and tokenization

✨ Features

Core Functionality

  • Interactive prompt with customizable display
  • Command execution with absolute and relative paths
  • Environment variable expansion ($VAR, $?)
  • Exit status tracking
  • Command history (using readline)
  • Quote handling (single ' and double " quotes)

Redirections

  • < - Input redirection
  • > - Output redirection (truncate)
  • >> - Output redirection (append)
  • << - Heredoc (read until delimiter)

Pipes

  • Multiple pipes support: cmd1 | cmd2 | cmd3
  • Proper file descriptor handling

Signal Handling

  • Ctrl+C - Interrupt current command
  • Ctrl+D - Exit shell (EOF)
  • Ctrl+\ - Quit signal (ignored in interactive mode)

πŸš€ Installation

Prerequisites

  • GCC or Clang compiler
  • Make
  • Readline library

Installing Readline

On Ubuntu/Debian:

sudo apt-get install libreadline-dev

On macOS:

brew install readline

Building the Project

  1. Clone the repository:
git clone https://github.com/codam-minishell-team/minsh.git
cd minsh
  1. Compile the project:
make
  1. Run the shell:
./minishell

Makefile Commands

  • make - Compile the project
  • make clean - Remove object files
  • make fclean - Remove object files and executable
  • make re - Recompile the project

πŸ’» Usage

Starting the Shell

Simply run the executable:

./minishell

You'll be greeted with a prompt showing your username and hostname:

user@hostname:~$

Basic Examples

Run simple commands:

minishell$ ls -la
minishell$ echo "Hello, World!"
minishell$ pwd

Use pipes:

minishell$ ls -l | grep minishell | wc -l
minishell$ cat file.txt | grep pattern | sort | uniq

Redirections:

minishell$ echo "test" > output.txt
minishell$ cat < input.txt
minishell$ ls -l >> logfile.txt

Heredoc:

minishell$ cat << EOF
> line 1
> line 2
> EOF

Environment variables:

minishell$ echo $HOME
minishell$ echo $?
minishell$ export MY_VAR=hello
minishell$ echo $MY_VAR

Quotes:

minishell$ echo 'single quotes: $HOME is not expanded'
minishell$ echo "double quotes: $HOME is expanded"

πŸ”§ Built-in Commands

Minishell implements the following built-in commands:

echo

echo [-n] [string ...]

Display a line of text. The -n option omits the trailing newline.

cd

cd [directory]

Change the current directory. If no argument is provided, changes to the home directory.

pwd

pwd

Print the current working directory.

export

export [NAME[=VALUE] ...]

Set or display environment variables.

unset

unset [NAME ...]

Remove environment variables.

env

env

Display all environment variables.

exit

exit [n]

Exit the shell with optional exit status n.

πŸ“ Project Structure

This project follows a structured directory layout to maintain clarity and organization. Below is a breakdown of each directory and its purpose:

β”œβ”€β”€ build                   intermediate build files (e.g., *.o) created by make
β”œβ”€β”€ docs                    project documentation
β”œβ”€β”€ includes                header files
β”œβ”€β”€ libs                    third-party libraries
β”œβ”€β”€ scripts                 scripts for setup and other tasks
β”œβ”€β”€ sources                 C source files
β”œβ”€β”€ tests                   contains test files
β”œβ”€β”€ .gitignore              specifies files and directories to be ignored by git
β”œβ”€β”€ Makefile                build instructions for the project
└── README.md               project overview and documentation

Folder Details

  • build: Stores intermediate build files, typically object files (*.o), generated during the build process.
  • docs: Contains project documentation, including design documents, guides, or API references.
  • includes: Holds header files (*.h), defining interfaces and shared declarations.
  • libs: Contains third-party libraries that the project depends on.
  • scripts: Includes utility scripts for setup, automation, or other project-related tasks.
  • sources: Source code files, primarily C files (*.c). This is where the application's logic resides.
  • tests: Test files for validating project functionality.

Additional Files

  • .gitignore: Defines files and folders that should be ignored by Git to keep the repository clean.
  • Makefile: Provides build commands and automates compilation steps.
  • README.md: Offers an overview of the project, including setup instructions, usage, and contribution guidelines.

Feel free to explore the directories for more details on each component of the project.

πŸ” Technical Details

Parser Architecture

The shell uses a multi-stage parsing approach:

  1. Scanner - Performs lexical analysis
  2. Tokenizer - Converts input into tokens
  3. Evaluator - Processes quotes, variables, and special characters
  4. Syntax Tree Builder - Constructs command pipeline structure

Memory Management

  • All dynamically allocated memory is properly freed
  • Valgrind-clean implementation (no memory leaks)
  • Custom cleanup functions for complex data structures

Signal Handling

Signals are handled differently based on execution context:

  • Interactive mode
  • Command execution
  • Heredoc input

Environment Variables

  • Custom implementation of getenv, setenv, and unsetenv
  • Environment variable expansion in double quotes
  • Exit status accessible via $?

πŸ‘₯ Contributors

Roman Zvir - GitHub Valeriia Krasnianska – GitHub School 42 Project


Note: This project is part of the 42 school curriculum and is intended for educational purposes. It implements a subset of bash functionality and may not include all features of a production shell.

About

minsh

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •