Epitech · 1st year · Solo, then as a team
Minishell → 42sh
A Unix shell written in C, first on my own, then extended as a team into 42sh.
- C
- Unix
- AST
- Period
- February – May 2025
- Team
- Minishell 1 and 2 solo, 42sh as a team of three with Ethanlsbr and enzo.ruffino
- Code
- Private repository
The project
One shell, built in three steps. Minishell 1 reads a command, looks the executable up in the PATH, runs it, and manages the environment with cd, setenv, unsetenv, env and exit.
Minishell 2 adds a tokenizer and an abstract syntax tree: pipes, input and output redirections, chained commands.
42sh takes this base further as a team, towards a real shell: ; and && separators, aliases, where, which, return values.
My part
- Minishell 1 and 2, on my own
- The base of 42sh, taken from my minishell 2: syntax tree, execution
- The ; and && separators in 42sh
- The fix for the crash when running a directory
What was hard
- Moving from a loop that runs one command to a real syntax tree, so that pipes, redirections, ; and && combine without special cases.
- Wiring file descriptors between processes correctly with pipe, fork and dup2, without leaving one open.
- Keeping memory under control in C: a good part of the history fixes segfaults and Valgrind errors.
What I learned
- How a shell handles a line: splitting it into tokens, building the tree, then executing it recursively.
- What fork, execve, wait and signals really do, and how to report a crash the way a real shell does.
- Writing code that others build on: my minishell 2 became the base of the team project.
Try it.
What a command line becomes
The path of ls -l | grep src > out.txt through the shell.
Step 1
Reading
The shell reads the line, or a file of commands when the input is not a terminal.
Step 2tokenizer
Tokens
ls, -l, |, grep, src, >, out.txt: words and operators.
Step 3AST
Syntax tree
Operators become nodes: ; and && at the top, then pipes, then redirections.
Step 4pipe · dup2
Wiring
A pipe links the output of ls to the input of grep, and the output of grep is redirected to the file.
Step 5fork · execve
Execution
Each command runs in its own process. The shell waits for it to finish and keeps the return value.