This project has been created as part of the 42 curriculum by mariacos and andcardo.
minishell is a simple Unix shell written in C built as part of the 42 school curriculum. The goal is to reproduce the core behavior of bash giving us a deep understanding of how a shell actually works under the hood. From reading user input, to parsing commands, to executing processes.
The shell supports:
- An interactive prompt that displays while waiting for a new command.
- Command history (navigate with arrow keys).
- Execution of commands using the
PATHenvironment variable, or with relative/absolute paths. - Single quotes
': prevent all interpretation of the enclosed characters. - Double quotes
": prevent interpretation except for$(variable expansion). - Redirections:
<: redirect input.>: redirect output.>>: append output.<<: heredoc (read input until a delimiter line is found).
- Pipes
|: connect the output of one command to the input of the next (we used the knowledge we gained by doing the project Pipex in the Milestone 2). - Environment variable expansion with
$VARand$?(last exit status). - Signal handling:
Ctrl+C,Ctrl+D,Ctrl+\behave like in bash. - Built-in commands implemented from scratch:
| Command | Description |
|---|---|
echo |
Print text (-n flag supported) |
cd |
Change directory |
pwd |
Print current working directory |
export |
Set or display environment variables |
unset |
Remove environment variables |
env |
Print all environment variables |
exit |
Exit the shell with an optional exit code |
Input (readline)
│
▼
Lexer: splits the input into tokens (words, pipes, redirections...) and identify syntax errors
│
▼
Parser: builds a list of command structures from the tokens
│
▼
Expansion: expands $VAR, $?, and handles quotes
│
▼
Execution: runs builtins directly, forks/execs external commands, handles pipes and redirections
- GCC compiler
- GNU
make readlinelibrary
On Debian/Ubuntu, install readline with:
sudo apt-get install libreadline-devThis will compile the project and produce the minishell executable.
Make targets:
| Target | Description |
|---|---|
make |
Build the project |
make clean |
Remove object files |
make fclean |
Remove object files and the executable |
make re |
Full recompile from scratch |
make valgrind |
Run with valgrind (leak check, suppression for readline) |
./minishellThe shell does not accept arguments. Once running you will see a prompt where you can type commands just like in bash.
# Basic command
$ ls -la
# Pipe
$ ls | grep .c | wc -l
# Redirections
$ echo "hello" > file.txt
$ cat < file.txt >> output.txt
# Heredoc
$ cat << EOF
> some text
> EOF
# Variable expansion
$ export NAME=world
$ echo "Hello $NAME"
Hello world
# Exit status
$ ls nonexistent
$ echo $?
2
# Built-ins
$ cd ..
$ pwd
$ export MY_VAR=42
$ unset MY_VAR
$ env
$ exit 0- Crafting Interpreters Representing Code
- Writing Your Own Shell (Purdue)
- Minishell: Building a Mini Bash (Medium)
AI was used during this project as a learning resource to better understand bash behavior, clarify concepts, and explore how things work under the hood.
mariacos | andcardo | 42 Lisboa