42sh
A Unix shell written in C, tcsh-style: pipes, logic operators, globbing, aliases, variables, line editing and history, checked against Bash.
- Period
- Apr 2025 – May 2025
- Role
- Globbing, aliases, variables and tests
- Team
- 5 people
- Status
- archived
Stack and tags
Demo
Transcript (final screen)
/home/demo/42sh
ls: cannot access 'nowhere': No such file or directory
(15:03) demo@~/42sh[2] $> setenv GREETING hello ; env | grep GREETING
GREETING=hello
(15:03) demo@~/42sh[0] $> unsetenv GREETING ; env | grep GREETING || echo gone
gone
(15:04) demo@~/42sh[0] $> echo one > notes.txt ; echo two >> notes.txt ; cat < notes.txt
one
two
(15:04) demo@~/42sh[0] $> ls src/*/*.c | wc -l ; echo {red,green,blue}
38
red green blue
(15:04) demo@~/42sh[0] $> alias ll "ls -1" ; ll include
llists.h
minishell.h
screen.h
tests.h
(15:04) demo@~/42sh[0] $> repeat 2 echo hi ; which cd ; where ls
hi
hi
cd: shell built-in command.
/usr/bin/ls
/bin/ls
(15:04) demo@~/42sh[0] $> notacommand
notacommand: Command not found.
(15:04) demo@~/42sh[1] $> echo "unclosed
Unmatched '"'.
(15:04) demo@~/42sh[1] $> ls >
Missing name for redirect.
(15:04) demo@~/42sh[1] $> A shell is the program you type commands into, and it is a small language of its own: ls *.c | wc -l && echo done is read, expanded and run by it. 42sh is our own, modelled on tcsh, built from scratch in C in about five weeks by a team of five.
How it works
A command line is first turned into a tree: what runs first, what depends on what, where the output goes. Before anything runs, the shell expands it, so *.c becomes the list of matching files and aliases are replaced. It has variables, the usual built-in commands, line editing and history when you type, and it stays quiet and scriptable when it is not talking to a person.
To be sure it behaves like a real shell, the tests run the same commands in 42sh and in Bash and compare the results.
What I took from it
- Copying a reference implementation’s output is a better test than reading the manual.
- Turning text into a tree first makes every later feature smaller.
Built with
Théo Futol@theo-futolSaint-Denis, Réunion3rd-year student at @Epitech.GitHubLinkedIn
Arthur Girardin--Calbe@arthur-girardin-calbeRéunionNo bio yet.GitHubLinkedIn
LOYER Aurélien@aurelien-loyerReunion IslandSecond-year student at Epitech Réunion – Class of @EpitechPGEPromo2029.GitHubWebsiteLinkedIn
- Joachim IsmaelNo bio yet.