Skip to content

πŸ† Minishell Project is a 42 project that recreates a simplified version of Bash. It handles command execution, input/output redirection, and environment variables, offering a deeper understanding of shell internals and process management.

Notifications You must be signed in to change notification settings

samir-ouaammou/Mini-Shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Minishell - 1337 School Project

πŸ”Ή Introduction

Minishell is a simplified Unix shell that mimics the behavior of Bash. It implements core functionalities such as command execution, pipelines, redirections, and environment variable handling. This project leverages system programming techniques and key system calls like execve(), fork(), pipe(), dup2(), and readline(), etc....


πŸ—οΈ Key Components

πŸ”Έ Lexer (Tokenizer)

Breaks user input into meaningful tokens.

Identifies commands, arguments, operators ((), ||, &&, |, >, <, >>, <<), and environment variables ($HOME).

Example:
ls | wc -l || cat file.txt && echo "Hello world!"

This is tokenized into:
[ls] [|] [wc -l] [||] [cat file.txt] [&&] [echo "Hello world!"]

AST Tree Representation

For the command:
[ls] [|] [wc -l] [||] [cat file.txt] [&&] [echo "Hello world!"]

                &&
             /      \
           ||       echo "Hello world!"
         /    \
       |     cat file.txt
    ls   wc -l  

βš™οΈ System Calls

  • Process Management: fork(), execve(), wait(), waitpid(), exit()
  • File & Directory Operations: open(), read(), write(), close(), opendir(), readdir(), stat(), lstat(), unlink(), access(), getcwd(), chdir()
  • Redirection & Pipes: dup(), dup2(), pipe()
  • Signal Handling: signal(), sigaction(), kill()
  • Memory Management: malloc(), free()
  • Error Handling: strerror(), perror()
  • Environment Variables: getenv()

⚑ Operators & Features

πŸ”Ή Redirections

  • < - Redirect input:
    Example: cat < file.txt

  • > - Redirect output:
    Example: ls > output.txt

  • >> - Append output:
    Example: ls >> output.txt

  • << - Here document:
    Example: cat << EOF

πŸ”Ή Pipes

  • | - Connects the output of one command to the input of another:
    Example: ls | grep minishell

πŸ”Ή Logical Operators

  • && - Execute next command if the previous succeeds:
    Example: ls && echo "Success"

  • || - Execute next command if the previous fails:
    Example: ls || echo "Failed"

πŸ”Ή Wildcards

  • * - Matches any number of characters:
    Example: ls *.txt

πŸ”Ή Environment Variables

  • $ - Expands environment variables:
    Example: echo $HOME

  • $? - Expands to the last command’s exit status:
    Example: echo $?

  • $USER - Expands to the current user:
    Example: echo $USER


πŸ“Œ Built-in Commands

  • cd - Change directory
  • echo - Print messages
  • export - Set environment variables
  • unset - Remove environment variables
  • env - Print environment variables
  • exit - Terminate the shell

πŸ“Œ Signal Handling

Manages signals like SIGINT (Ctrl+C), SIGQUIT (Ctrl+D), and SIGTSTP (Ctrl+Z).

Uses system calls: signal(), sigaction(), kill()


πŸ—οΈ Project Structure

minishell/                 
β”œβ”€β”€ builtins/              # Built-in commands           
β”œβ”€β”€ execution/             # Command execution logic          
β”œβ”€β”€ libft/                 # Custom library functions        
β”œβ”€β”€ malloc/                # Memory management logic        
β”œβ”€β”€ parsing/               # Parsing logic          
β”œβ”€β”€ signals/               # Signal handling logic            
β”œβ”€β”€ wildcards/             # Wildcard expansion logic          
β”œβ”€β”€ minishell.c            # Main entry point          
β”œβ”€β”€ minishell.h            # Main header file          
β”œβ”€β”€ Makefile               # Build script             
β”œβ”€β”€ README.md              # Documentation                     

πŸ› οΈ Installation

  1. Clone the repository to your local machine:

    git clone https://github.com/samir-ouaammou/Mini-Shell
  2. Navigate to the project directory:

    cd Mini-Shell/Project
  3. Compile the source files using make:

    make 
  4. Clean up compiled files:

    make clean
  5. To remove all object files and the executable:

    make fclean
  6. To recompile the project from scratch:

    make re
  7. Run the program:

    ./minishell

πŸš€ Features to Implement

βœ… Command Execution (Using fork(), execve(), and redirections)
βœ… Pipes & Redirections (Handling |, <, >, >>, <<)
βœ… Environment Variables (Expanding $HOME, $PATH, $?)
βœ… Built-in Commands (cd, echo, pwd, export, unset, env, exit)
βœ… Error Handling (Invalid commands, syntax errors)
βœ… Signal Handling (Ctrl+C, Ctrl+D)
βœ… Memory Management (Avoiding memory leaks)


πŸ“Œ Operators to Handle

βœ… () - Subshell execution
βœ… && - AND Operator
βœ… || - OR Operator
βœ… | - Pipe Operator
βœ… < - Input Redirection
βœ… > - Output Redirection
βœ… << - Here Document
βœ… >> - Append Output Redirection
βœ… $ - Variable Expansion
βœ… $? - Last Exit Status
βœ… $HOME - Home Directory Expansion


πŸ“Œ Next Steps

βœ… Add Logical Operators (&&, ||)
βœ… Implement Command History (readline())
βœ… Improve Error Messages
βœ… Implement Job Control (fg, bg)


πŸ† Key Achievements

  • Developed a custom shell with support for basic commands, pipelines, and redirections.
  • Mastered the use of a lexer to tokenize input and a parser to interpret and structure commands.
  • Created and managed tokens to represent individual components of commands for easier processing.
  • Built an efficient abstract syntax tree (AST) to represent and execute parsed commands and operators. 🌳
  • Used fork() and wait() to manage process execution and synchronization.
  • Implemented pipe() for connecting processes and enabling inter-process communication.
  • Successfully utilized exec() to execute commands and launch programs. πŸš€

πŸš€ Next Steps

  • Starting the Philosophers, cub3D, and Net_Practice projects to deepen my understanding of concurrency, graphics, and networking.
  • Continuing to refine my knowledge of system-level programming and dynamic memory management. πŸ’‘
  • Expanding my skills in C++ to explore object-oriented programming and advanced concepts. πŸš€
  • Applying the experience gained to tackle increasingly complex and challenging problems. 🌍

🎯 Reflection

The Minishell project was a fascinating journey into the heart of shell programming. It offered me a hands-on experience with process creation, command parsing, and system calls that are fundamental to building a shell. πŸ–₯️ The challenges of implementing pipes, redirections, and error handling pushed me to think critically about how to manage resources and memory efficiently. πŸ’‘ I worked on this project as part of a group of two students, which provided valuable experience in collaborative problem-solving and team communication. By diving deep into system-level programming, I’ve gained invaluable insights into how shells operate under the hood. I’m eager to apply this knowledge to more advanced projects. This experience has fueled my growth as a developer, and I’m excited to continue exploring new topics and pushing my limits. πŸš€


Thank you for checking out my minishell project! πŸš€ Stay tuned for more updates as I continue to enhance my skills and tackle new challenges.

About

πŸ† Minishell Project is a 42 project that recreates a simplified version of Bash. It handles command execution, input/output redirection, and environment variables, offering a deeper understanding of shell internals and process management.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •