This project is our implementation of a simplified Shell, which is part of the 42 coding school curriculum. As a team of two, we worked together to create a minimalist version of bash that can interpret commands, handle executables, and implement various shell functionalities. This project required us to dive deep into processes, file descriptors, and command interpretation.
Our implementation follows a structured pipeline approach with distinct processing stages from prompt display to command execution. This solution handles various shell features like redirections, pipes, environment variables, and built-in commands.
Minishell
- Language: C
- License: MIT License
Click to expand
The Minishell project involves creating a simplified version of a Unix shell, specifically taking bash as a reference. A shell serves as the primary interface between the user and the operating system, interpreting commands and executing programs.
We worked together to create a shell that handles:
- Command interpretation π
- Program execution β‘
- Shell built-ins π οΈ
- Signal handling π¨
This collaborative project challenged us to understand how shells work under the hood, dealing with processes, file descriptors, and command parsing while providing a smooth user experience.
-
π₯οΈ Interactive Prompt: Displays a prompt when waiting for new commands.
-
π Command History: Maintains a working history of commands.
-
π Path Resolution: Searches and launches executables based on PATH or given paths.
-
βοΈ Command Line Editing: Basic command line editing capabilities.
-
π£ Special Character Handling:
- Handles quotes (
'and") (unmatching quotes simply displays an error message) - Manages redirections (
<,>,<<,>>) - Processes pipes (
|) - Interprets environment variables (
$)
- Handles quotes (
-
π οΈ Built-in Commands:
echowith-noptioncdwith relative or absolute pathpwdwithout optionsexportto set environment variablesunsetto remove environment variablesenvto display environmentexitto terminate the shell
-
π¨ Signal Handling:
Ctrl-C: Displays a new prompt on a new lineCtrl-D: Exits the shellCtrl-\: No action
Our minishell implementation follows a clear sequential workflow that transforms user input into executed commands:
We begin by displaying a customized prompt to the user using the readline library.
We use the readline library to capture user input and manage command history.
Before further processing, we validate the input to ensure it follows proper shell syntax, checking for:
- Matching quotes
- Valid operator usage
- Proper command structure
Once syntax is verified, we break down the input string into meaningful tokens.
For commands with heredoc operators (<<), we collect multi-line input until a delimiter is encountered.
When commands include pipes, we set up the necessary pipes and file descriptors to connect command outputs to inputs.
We process input/output redirections by opening appropriate files and redirecting standard I/O.
Finally, we execute the commands, distinguishing between built-ins and external programs:
- Built-ins are executed directly within the shell process
- External commands are executed through forking and execve
This structured workflow ensures each command is properly processed and executed while handling all shell functionalities.
This project was developed collaboratively by two 42 students:
- Melanie Reis (melaniereis) - Mainly focused on parsing, signal handling, tokenization, redirections, and heredocs.
- Miguel Meireles (m3irel3s) - Mainly focused on command execution, shell variables, and pipes.
We worked closely together throughout the project, often collaborating on tasks and sharing responsibilities to ensure a cohesive and well-structured implementation. Our collaborative approach involved regular code reviews, task division, and occasional pair programming sessions. Our teamwork was essential in achieving a comprehensive and functional Minishell implementation.
Before running the project, make sure you have the following installed:
ccor any compatible C compilermakeutility to build the projectreadlinelibrary for command line editing
- Clone the repository:
git clone https://github.com/m3irel3s/41_Minishell.git- Navigate to the project directory and build:
cd minishell
makeThis will compile the project and generate the executable minishell.
To start the shell, simply run:
./minishellOur Minishell supports the following syntax elements:
-
Simple Commands:
ls -l,echo hello -
Redirections:
- Input:
cat < file.txt - Output:
ls > output.txt - Append:
echo hello >> log.txt - Heredoc:
cat << EOF
- Input:
-
Pipes:
ls -l | grep ".c" -
Environment Variables:
echo $HOME -
Exit Status:
echo $?
- Basic command execution:
minishell> ls -la- Using redirections:
minishell> cat < input.txt > output.txt- Using pipes:
minishell: > ls -l | grep ".c" | wc -l- Using built-in commands:
minishell: > cd /tmp
minishell: > pwd
minishell: > echo Hello, $USER!- Environment variable operations:
minishell: > export NAME=value
minishell: > env | grep NAME
minishell: > unset NAMETo ensure your shell is working correctly, test the following scenarios:
- Basic Commands: Test simple Unix commands like
ls,cat,grep. - Built-ins: Test all built-in commands with various arguments.
- Redirections: Test all types of redirections with different combinations.
- Pipes: Test multiple piped commands with varying complexity.
- Quotes: Test commands with single and double quotes, including nested quotes.
- Environment Variables: Test variable expansion in different contexts.
- Signals: Test how the shell responds to Ctrl-C, Ctrl-D, and Ctrl-.
- Commands with very long arguments
- Handling of non-existent commands
- Multiple redirections in a single command
- Nested quotes and escape characters
- Empty input
- Invalid syntax
Contributions to improve the Minishell project are welcome. Please follow these steps:
- Fork the repository
- Create a new branch (
git checkout -b feature/improvement) - Make your changes
- Commit your changes (
git commit -m 'Add some feature') - Push to the branch (
git push origin feature/improvement) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.