Skip to content

A compact recreation of the bash shell, applying key concepts such as file descriptors and process management.

Notifications You must be signed in to change notification settings

voloshynm/minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

189 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Minishell

A compact recreation of the bash shell, implementing key concepts such as file descriptors, process management, and command parsing. This 42 School project demonstrates systems programming fundamentals through building a functional UNIX shell from scratch.

Demo Language

🐚 About

Minishell is a simplified implementation of bash that handles essential shell operations including command execution, pipes, redirections, and built-in commands. Built entirely in C with strict memory management and following the 42 coding standards.

✨ Features

  • Interactive Prompt: Command-line interface with history support
  • Command Execution: System executables and local programs
  • Built-in Commands: Essential shell commands implemented natively
  • Pipes & Redirections: Full I/O redirection and command chaining
  • Environment Variables: Variable expansion and manipulation
  • Signal Handling: Proper Ctrl+C, Ctrl+D, and Ctrl+\ behavior
  • Quote Parsing: Single and double quote handling
  • Error Management: Comprehensive error handling and exit codes

πŸ› οΈ Supported Features

Built-in Commands

| Command | Description                  | Options                 |
|---------|------------------------------|-------------------------|
| `echo`  | Display text                 | `-n` (no newline)       |
| `cd`    | Change directory             | Relative/absolute paths |
| `pwd`   | Print working directory      | None                    |
| `export`| Set environment variables    | Variable assignment     |
| `unset` | Remove environment variables | Variable names          |
| `env`   | Display environment          | None                    |
| `exit`  | Exit shell                   | Optional exit code      |

Operators & Redirections

| Oper | Function                     | Example                            |
|------|------------------------------|------------------------------------|
| `|`  | Pipe output to next command  | `ls | grep .c`                     |
| `>`  | Redirect output              | `echo "text" > file.txt`           |
| `>>` | Append output                | `date >> log.txt`                  |
| `<`  | Redirect input               | `sort < data.txt `                 |
| `<<` | Here document                | `cat << EOF`                       |
| `&&` | Execute if previous succeeds | `make && ./program`                |
| `||` | Execute if previous fails    | `test -f file || echo "not found"` |
| `&`  | Run command in background    | `sleep 10 &`                       |
minishell$ cat  processed.txt
minishell$ ls >> file_list.txt

Environment Variables

  • Variable Expansion: $HOME, $USER, $PATH
  • Exit Status: $? expands to last command's exit code
  • Custom Variables: Set and use custom environment variables
  • Quote Handling: Different expansion behavior in single vs double quotes
minishell$ export MY_VAR="test value"
minishell$ echo $MY_VAR
test value

minishell$ echo "Exit code: $?"
Exit code: 0

Signal Handling

  • Ctrl+C (SIGINT):
    • If foreground process running: send SIGINT to child
    • If no foreground process: display new prompt
  • Ctrl+D: Exit minishell cleanly
  • Ctrl+Z (SIGTSTP): Stop foreground process and show PID
  • **Ctrl+**: No action (ignored)

Here Documents

minishell$ cat  This is a here document
> Multiple lines supported
> EOF
This is a here document
Multiple lines supported

πŸ—οΈ Project Structure

minishell/
β”œβ”€β”€ src/                    # Source files
β”‚   β”œβ”€β”€ main.c             # Program entry point
β”‚   β”œβ”€β”€ parser/            # Command parsing logic
β”‚   β”‚   β”œβ”€β”€ lexer.c        # Tokenization
β”‚   β”‚   β”œβ”€β”€ parser.c       # Syntax analysis
β”‚   β”‚   └── expander.c     # Variable expansion
β”‚   β”œβ”€β”€ executor/          # Command execution
β”‚   β”‚   β”œβ”€β”€ executor.c     # Main execution logic
β”‚   β”‚   β”œβ”€β”€ pipes.c        # Pipe handling
β”‚   β”‚   └── redirections.c # I/O redirection
β”‚   β”œβ”€β”€ builtins/          # Built-in commands
β”‚   β”‚   β”œβ”€β”€ echo.c         # Echo implementation
β”‚   β”‚   β”œβ”€β”€ cd.c           # Directory change
β”‚   β”‚   β”œβ”€β”€ pwd.c          # Working directory
β”‚   β”‚   β”œβ”€β”€ export.c       # Environment export
β”‚   β”‚   β”œβ”€β”€ unset.c        # Variable removal
β”‚   β”‚   β”œβ”€β”€ env.c          # Environment display
β”‚   β”‚   └── exit.c         # Shell exit
β”‚   β”œβ”€β”€ signals/           # Signal handling
β”‚   └── utils/             # Utility functions
β”œβ”€β”€ includes/
β”‚   └── minishell.h        # Header file
β”œβ”€β”€ Makefile              # Build configuration
└── README.md

πŸ”§ Technical Implementation

Core Components

Lexical Analysis: Tokenizes input into meaningful components (commands, operators, arguments)

Syntax Parsing: Builds command structures and validates syntax

Variable Expansion: Processes environment variables and special parameters

Process Management: Handles fork/exec for command execution

File Descriptor Management: Manages pipes and redirections efficiently

Memory Management: Ensures zero memory leaks with proper cleanup

Key Algorithms

Command Pipeline: Implements pipe chains with proper file descriptor handling

Quote Processing: Handles nested quotes and escape sequences

Path Resolution: Searches PATH environment variable for executables

Signal Safety: Implements async-signal-safe signal handlers

πŸ§ͺ Testing

Manual Testing

# Test basic commands
./minishell
minishell$ echo test
minishell$ pwd
minishell$ cd ..

# Test pipes and redirections
minishell$ ls | wc -l
minishell$ echo "test" > file.txt
minishell$ cat < file.txt

# Test environment variables
minishell$ export TEST=value
minishell$ echo $TEST
minishell$ unset TEST

Edge Cases

  • Empty commands and multiple spaces
  • Unclosed quotes and syntax errors
  • Invalid file permissions
  • Signal interruption during execution
  • Large command pipelines

🚫 Limitations

Minishell does not support:

  • Wildcards (*, ?, [])
  • Command substitution (`command` or $(command))
  • Logical operators (&&, ||)
  • Command separators (;)
  • Backslash escaping (\)
  • Job control and background processes (&)

🀝 Contributing

This is a 42 School project, but contributions for educational purposes are welcome:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/improvement)
  3. Commit your changes (git commit -m 'Add improvement')
  4. Push to the branch (git push origin feature/improvement)
  5. Open a Pull Request

πŸ“š Learning Resources

🎯 42 School Requirements

  • Norm Compliance: Follows 42 coding standards
  • Memory Management: Zero memory leaks (verified with Valgrind)
  • Error Handling: Comprehensive error management
  • Function Restrictions: Limited to approved system calls
  • Global Variables: Minimal global state usage

πŸ“„ License

This project is part of the 42 School curriculum. Use for educational purposes only.

πŸ‘€ Author

Maksym Voloshyn

Sergio Filipe

πŸ™ Acknowledgments

  • 42 School - Project specification and learning framework
  • GNU Bash Team - Reference implementation and documentation
  • UNIX Pioneers - Shell design principles and philosophy
  • Fellow 42 Students - Collaboration and peer learning

"Understanding shells means understanding UNIX" - Building minishell teaches the fundamentals of systems programming

Keywords: c-programming unix-shell 42-school systems-programming bash shell pipes redirections process-management

About

A compact recreation of the bash shell, applying key concepts such as file descriptors and process management.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors