Skip to content

BatuhanKas/minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🐚 MiniShell

Γ‰cole 42 Minishell Project - A simple Unix shell implementation that mimics the basic features of Bash.

πŸ“‹ Table of Contents

🎯 Overview

MiniShell is a Unix shell application developed as part of the Γ‰cole 42 curriculum. This project aims to develop a deep understanding of system programming, process management, file descriptors, and parsing.

The project allows users to execute system commands through a command-line interface, use pipes and redirections, and manage environment variables.

✨ Features

Command Execution

  • βœ… Finding and executing commands from PATH
  • βœ… Relative and absolute path support
  • βœ… Command argument handling

Redirections

  • βœ… < - Input redirection
  • βœ… > - Output redirection (overwrite)
  • βœ… >> - Output redirection (append)
  • βœ… << - Heredoc (read input until delimiter)

Pipe Operations

  • βœ… Pipe (|) support between multiple commands
  • βœ… Unlimited pipe chains
  • βœ… File descriptor management between pipes

Quotes and Special Characters

  • βœ… Single quotes (') - Ignore all metacharacters
  • βœ… Double quotes (") - Ignore metacharacters except $
  • βœ… Escape character (\) support

Environment Variables

  • βœ… $VAR - Variable expansion
  • βœ… $? - Exit status of last command
  • βœ… Variable expansion within double quotes

Signal Management

  • βœ… Ctrl-C - SIGINT (terminate current command)
  • βœ… Ctrl-D - EOF (exit shell)
  • βœ… Ctrl-\ - SIGQUIT (do nothing)

πŸ”§ Installation

Requirements

  • GCC compiler
  • GNU Make
  • readline library
# Install readline on macOS
brew install readline

# Install readline on Linux
sudo apt-get install libreadline-dev

Compilation

# Clone the repository
git clone [repository-url] minishell
cd minishell

# Compile
make

# Clean
make clean      # Remove object files
make fclean     # Remove all compiled files
make re         # Recompile

πŸš€ Usage

# Start MiniShell
./minishell

# Shell is running
MiniShell$ echo "Hello World"
MiniShell$ ls -la | grep minishell
MiniShell$ cat < input.txt > output.txt
MiniShell$ export MY_VAR=42
MiniShell$ echo $MY_VAR

πŸ“¦ Built-in Commands

echo

Prints text to the screen. The -n flag suppresses the newline character.

echo Hello World
echo -n "No newline"

cd

Changes the current working directory.

cd /path/to/directory
cd ..
cd ~
cd        # Goes to HOME directory

pwd

Prints the current working directory.

pwd

export

Creates or updates environment variables.

export VAR=value
export PATH=/usr/bin:$PATH
export          # List all exported variables

unset

Removes environment variables.

unset VAR

env

Lists all environment variables.

env

exit

Exits the shell. Optional exit code can be specified.

exit
exit 42

πŸ›  Technical Details

Memory Management

  • All dynamic memory allocations are properly freed
  • Valgrind can be used for memory leak checking
  • Temporary memory cleanup after each command

Process Management

  • Creating child processes with fork()
  • Executing commands with execve()
  • Process tracking with waitpid()
  • Process synchronization for pipes

Parsing

  • Tokenization and lexical analysis
  • Parsing within and outside quotes
  • Special character handling
  • Syntax error checking

Error Management

  • Appropriate messages for syntax errors
  • Command not found (127)
  • Permission denied errors
  • Exit status codes

πŸ“ Project Structure

minishell/
β”œβ”€β”€ main.c                      # Main program loop and signal management
β”œβ”€β”€ Makefile                    # Compilation configuration
β”œβ”€β”€ include/
β”‚   └── minishell.h            # Header file and struct definitions
β”œβ”€β”€ built-in/                   # Built-in commands
β”‚   β”œβ”€β”€ built-in.c             # Built-in dispatcher
β”‚   β”œβ”€β”€ cd.c                   # cd command
β”‚   β”œβ”€β”€ echo.c                 # echo command
β”‚   β”œβ”€β”€ env.c                  # env command
β”‚   β”œβ”€β”€ exit.c                 # exit command
β”‚   β”œβ”€β”€ export.c               # export command
β”‚   β”œβ”€β”€ pwd.c                  # pwd command
β”‚   └── unset.c                # unset command
β”œβ”€β”€ execute/                    # Command execution
β”‚   β”œβ”€β”€ add_pipes.c            # Pipe structure creation
β”‚   β”œβ”€β”€ execute_command.c      # Command execution
β”‚   β”œβ”€β”€ next_process.c         # Pipe chain management
β”‚   └── redir_execute.c        # Redirection execution
β”œβ”€β”€ init_and_find_path/         # Initialization and PATH
β”‚   β”œβ”€β”€ find_env.c             # Environment variable lookup
β”‚   β”œβ”€β”€ find_path.c            # Command search in PATH
β”‚   └── ms_init.c              # Shell initialization
β”œβ”€β”€ redirections/               # Redirection operations
β”‚   β”œβ”€β”€ redir_heredoc.c        # Heredoc processing
β”‚   β”œβ”€β”€ redir_order.c          # Redirection ordering
β”‚   β”œβ”€β”€ redir_run.c            # Redirection execution
β”‚   └── redir_set.c            # Redirection setup
β”œβ”€β”€ syntax/                     # Parsing and syntax
β”‚   β”œβ”€β”€ add_cmd.c              # Command token processing
β”‚   β”œβ”€β”€ dollar_parse.c         # Variable expansion
β”‚   └── quate_parse.c          # Quote handling
└── libft/                      # Utility function library
    β”œβ”€β”€ ft_split.c
    β”œβ”€β”€ ft_strjoin.c
    └── ... (other utility functions)

πŸ’‘ Examples

Simple Commands

MiniShell$ ls -la
MiniShell$ cat file.txt
MiniShell$ grep "pattern" file.txt

Using Pipes

MiniShell$ ls -la | grep minishell | wc -l
MiniShell$ cat file.txt | grep "error" | sort | uniq

Redirections

MiniShell$ echo "Hello" > output.txt
MiniShell$ cat < input.txt > output.txt
MiniShell$ ls -la >> log.txt

Heredoc

MiniShell$ cat << EOF
> Line 1
> Line 2
> EOF

Environment Variables

MiniShell$ export NAME="John"
MiniShell$ echo "Hello $NAME"
MiniShell$ echo "Exit code: $?"

Using Quotes

MiniShell$ echo 'Single quotes: $USER'
MiniShell$ echo "Double quotes: $USER"
MiniShell$ echo "Mixed 'quotes' work"

Complex Combinations

MiniShell$ < input.txt grep "error" | sort | uniq > output.txt
MiniShell$ export PATH=/bin:/usr/bin && ls | wc -l

πŸŽ“ Learning Outcomes

Throughout this project, in-depth knowledge was gained in the following topics:

  • System Programming: Fork, exec, wait system calls
  • Process Management: Creating and managing child processes
  • Pipes and IPC: Inter-process communication
  • File Descriptors: FD management with dup2, close
  • Signal Handling: SIGINT, SIGQUIT management
  • Parsing: Lexical analysis and tokenization
  • Memory Management: Malloc, free and leak prevention
  • UNIX Shell: Understanding Bash behavior

⚠️ Known Limitations

  • Wildcard (*) expansion is not supported
  • History navigation (up/down arrows) is limited
  • Job control (bg, fg, jobs) is not supported
  • Subshells (parentheses usage) are not supported
  • Logical operators (&&, ||) are not supported

πŸ“ License

This project is part of the Γ‰cole 42 curriculum and was developed for educational purposes.

πŸ‘₯ Contributors

  • bkas - Developer
  • melihyil - Developer

Γ‰cole 42 | Minishell Projesi | 2024

About

A Unix shell implementation that mimics the basic features of Bash. Fully functional command-line interface with pipes, redirections, environment variables, and built-in commands. System programming and process management project written in C.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors