This project was created as part of the 42-SP curriculum by Luca (lbento) and Ian (iaratang).
This project is a simplified version of Unix shell written in C, developed as part of the 42 School curriculum. minishell reproduces the core behavior of bash, including tokenization, command execution, pipelines, redirections, environment variable expansion, and built-in commands — all built from scratch using low-level system calls.
The Minishell requires students to implement core shell functionalities such as:
- Displaying a dynamic command prompt
- Parsing and executing user input
- Handling environment variables
- Managing processes using fork, execve, and wait
- Implementing input and output redirections
>,>>,< - Supporting pipes
| - Handling signals like
ctrl+C,ctrl+D, andctrl+\ - Recreating essential built-in commands such as
echo,cd,pwd,export,unset,env, andexit
This project is a deep dive into how a real shell works under the hood.
Before compiling and running minishell, make sure your Linux machine has the following installed:
| Dependency | Purpose |
|---|---|
gcc (≥ 9) or cc |
C compiler (C99 standard) |
make |
Build automation |
readline library |
Line editing and input history |
libreadline-dev |
Development headers for readline |
Note: The readline library is mandatory. Without it, the project will not compile.
On a Debian/Ubuntu-based Linux system, install all required dependencies with:
sudo apt update
sudo apt install -y gcc make libreadline-devVerify that everything is installed correctly:
gcc --version
make --version
pkg-config --libs readline #should output something like: -lreadlineOn MacOS:
brew install readline makeVerify the setup before is correctly:
brew --prefix readline #should print the readline install path
ls $(brew --prefix readline)/include/readline/readline.h #header must existNote: The command brew --prefix readline command solver the correct path automatically regardless of whether the machine is Apple Silicon (M1/M2/M3) or Intel.
Clone the repository and compile the project:
git clone https://github.com/lbento/minishell.git
cd minishell
make #If you using macOs use: make macThis will generate the minishell executable in the project root directory.
To remove object files:
make cleanTo remove object files and the executable:
make fcleanTo recompile from scratch:
make reTo recompile and execute the minishell with valgrind:
make valLaunch the shell by executing:
./minishellIt will open with a custom prompt. From there, you can type commands like a regular Bash shell.
To exit the shell, type:
exitOr press Ctrl+D (sends EOF).
Below are some commands you can test inside minishell to explore its features:
Basic command execution:
ls -la
echo "Hello, 42!"
pwdEnvironment variables:
echo $HOME
export MY_VAR=hello
echo $MY_VAR
unset MY_VAR
echo $MY_VARBuilt-in commands:
cd /tmp
cd -
cd ~
pwd
env
exit 42Input and output redirections:
echo "writing to file" > output.txt
cat < output.txt
echo "appending line" >> output.txt
cat output.txt
ls > f1 > f2 > -laPipelines:
ls | grep .c
cat Makefile | grep minishell
echo "hello world" | wc -w
cat << a | cat << b | cat << c Combined pipeline with redirections:
ls -la | grep .c > c_files.txt
cat c_files.txtHere-document (heredoc):
cat << eof
line one
line two
eofExit status:
ls /nonexistent
echo $?
ls /tmp
echo $?
#press ctrl + C
echo $?Signals:
Ctrl+C— interrupts the current command or processCtrl+\— does nothingCtrl+D— exits the shell and process that receive EOF
cat
#press ctrl + C
cat
#press ctrl + \
cat
#press ctrl + D
sleep 90
#press ctrl + C
sleep 90
#press ctrl + \
sleep 90
#press ctrl + DQuoting:
echo "This is $USER inside double quotes"
echo 'This is $HOME inside single quotes'
echo "Multiple spaces preserved"The following resources were used during the development of this project and are recommended for anyone who wants to understand the concepts involved:
-
Bash Reference Manual — The official GNU Bash documentation, covering everything from syntax to built-ins and job control. https://www.gnu.org/software/bash/manual/bash.html
-
POSIX Shell Command Language — The formal POSIX specification for shell behavior, useful for understanding what is standard versus bash-specific. https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
-
Linux
manpages online — Quick reference for all system calls and library functions used in minishell (fork,execve,pipe,dup2,waitpid,readline, etc.). https://man7.org/linux/man-pages/
-
"The Linux Programming Interface" — Michael Kerrisk. The most comprehensive reference on Linux system programming, covering processes, file descriptors, signals, and more. https://man7.org/tlpi/
-
"Advanced Programming in the UNIX Environment" — W. Richard Stevens & Stephen Rago. A classic reference for UNIX systems programming in C.
- GNU Readline Library — Official documentation for the readline library used for input handling and history. https://tiswww.case.edu/php/chet/readline/rltop.html
Don’t despair, dear cadet — this project is truly challenging. Trust the process!