__ __ _ _ _ _ _ _
| \/ (_) (_) | | | | | | |
| \ / |_ _ __ _ ___| |__ | |__ ___| | |
| |\/| | | '_ \| / __| '_ \| '_ \ / _ \ | |
| | | | | | | | \__ \ | | | | | | __/ | |
|_| |_|_|_| |_|_|___/_| |_|_| |_|\___|_|_|
Minishell is a simplified Unix shell implemented in C as part of the 42 curriculum. It aims to replicate essential shell functionalities, providing a hands-on understanding of how shells work internally.
The project plan is available here.
- Minishell
- Why Minishell?
- Quick Demo
- π Features
- π¦ Installation
- π₯οΈ Usage
- π§© Built-in Commands
- π Wildcard Expansion
- ποΈ Project Structure
β οΈ Limitations- How It Works
- π€ Contributing
- π€ Authors
- π License
A fully functional Unix shell, written in C, featuring command parsing, pipelines, redirections, environment and wildcard expansion, and robust error/signal handling.
Minishell is more than a school projectβit's a demonstration of my ability to:
- Design and implement complex systems in C
- Work with low-level process management, memory, and signals
- Recreate core Unix shell features from scratch
- Write robust, maintainable, and well-documented code
- Debug, test, and handle edge cases in real-world scenarios
$ ./minishell
minishell$ echo Hello, world!
Hello, world!
minishell$ ls *.c | grep main
main.c
minishell$ export PATH=$PATH:/custom/bin
minishell$ exit
- Command execution: Run external programs with arguments.
- Built-in commands: Includes
cd
,echo
,pwd
,export
,unset
,env
, andexit
. - Redirections: Supports input (
<
), output (>
), and append (>>
) redirections. - Pipelines: Chain commands using the pipe operator (
|
). - Environment variable expansion: Handles
$VAR
and$?
expansions. - Wildcard expansion: Supports globbing with
*
(e.g.,ls *.c
). - Signal handling: Properly manages signals like
Ctrl+C
andCtrl+D
. - Error handling: Provides informative error messages for invalid commands or syntax.
- Memory management: Avoids memory leaks and ensures resource cleanup.
- Clone the repository:
git clone https://github.com/jcmspg/minishell.git cd minishell
- Build the project:
This will compile the source code and generate the
make
minishell
executable.
Start the shell by running:
./minishell
You can now enter commands as you would in a standard shell. Examples:
ls *.c | grep main > results.txt
export PATH=$PATH:/custom/bin
cat < input.txt | sort | uniq
echo "Hello, $USER!"
ls src/*.c
Debug Mode:
To see how your input is parsed and processed, run:
./minishell -d
This will print detailed debug information for each command entered.
You can now enter commands as you would in a standard shell. Examples:
ls *.c | grep main > results.txt
export PATH=$PATH:/custom/bin
cat < input.txt | sort | uniq
echo "Hello, $USER!"
ls src/*
Command | Description |
---|---|
cd [dir] |
Change the current directory |
echo [args...] |
Print arguments to standard output |
pwd |
Print the current working directory |
export [VAR=VAL] |
Set environment variables |
unset [VAR] |
Remove environment variables |
env |
List environment variables |
exit [status] |
Exit the shell |
Minishell supports wildcard expansion (globbing) in command arguments, similar to Bash:
*
matches any sequence of characters (except hidden files).
Examples:
ls *.c
echo src/*.h
src/
builtins/ # Built-in command implementations
cmd_as_regular_user/
env/ # Environment variable management
frees/ # Memory management
history/ # Command history
inits/ # Shell initialization
parsing/ # Command parsing and tokenization
pipes/ # Pipe handling
redirections/ # Redirection logic
run/ # Main shell loop and execution
signals/ # Signal handling
utils/ # Utility functions
validations/ # Command and argument validation
wildcards/ # Wildcard/globbing logic
Makefile
README.md
- Not Supported:
- Logical OR (
||
), command separator (;
), and process ID expansion ($$
) are not implemented.
- Logical OR (
- No job control:
- Background jobs (
&
) and job control commands are not available.
- Background jobs (
- No advanced Bash features:
- No subshells, process substitution, or advanced scripting.
Minishell follows a classic shell architecture, processing user input in a loop and executing commands step by step:
+---------------------+
| 1. Prompt & Input |
+---------------------+
|
v
+---------------------+
| 2. Lexing & Parsing |
+---------------------+
|
v
+---------------------+
| 3. Expansion |
| (env, wildcards) |
+---------------------+
|
v
+---------------------+
| 4. Execution |
| (builtins/ext) |
+---------------------+
|
v
+---------------------+
| 5. Cleanup & Loop |
+---------------------+
- The shell displays a prompt and waits for user input.
- Input is read line-by-line, handling signals like
Ctrl+C
andCtrl+D
.
- The input line is split into tokens (words, operators, etc.).
- The parser builds a command structure, identifying commands, arguments, redirections, and pipes.
- Environment variables (e.g.,
$HOME
,$?
) are expanded. - Wildcards (
*
,?
) are expanded to matching filenames using globbing.
- Built-in commands (
cd
,echo
, etc.) are executed directly in the shell process. - External commands are executed by forking a child process and using
execve
. - Redirections and pipes are set up as needed.
- Resources are freed, and the shell returns to the prompt for the next command.
Key Components:
- Parsing: Converts user input into structured commands.
- Expansion: Handles variable and wildcard expansion.
- Execution: Runs built-in or external commands, managing processes and I/O.
- Signal Handling: Ensures the shell responds gracefully to user interrupts.
- Error Handling: Detects and reports syntax or runtime errors.
This modular approach makes it easy to extend Minishell with new features or built-ins, and ensures robust, predictable behavior similar to standard Unix shells.
Contributions are welcome! Please open issues or submit pull requests for improvements or bug fixes.
- JoΓ£o (@jcmspg)
- Nuno (@nunnoandrezo)
This project is licensed under the MIT License.
The project plan is available here.