A small Bash-like shell written from scratch in C, as part of the 42 school curriculum (minishell).
It reads a line through GNU readline, tokenises it, builds a command list, runs pipelines and redirections, and takes care of its own environment, exit statuses, signals and memory.
The prompt is 🜁 Y-Shell>; the banner prints the repository address.
| Area | Implemented |
|---|---|
| Line editing | GNU readline: history, arrow keys, Ctrl+R search |
| Tokenising | quotes (single and double), escapes, $VAR and $? expansion |
| Pipelines | any number of commands joined with ` |
| Redirections | <, >, >>, << (heredoc) |
| Heredocs | optional variable expansion, interrupted cleanly with Ctrl+C |
| Environment | own linked list, SHLVL, export, unset, lookup for $VAR |
| Builtins | echo (with -n), cd, pwd, export, unset, env, exit |
| Exit status | stored per command, $? reflects the last pipeline |
| Signals | Ctrl+C at the prompt clears the line; SIGQUIT ignored |
| Memory | every allocation goes through a small garbage collector |
These are outside the 42 subject and deliberately absent:
;,&&,||and command grouping- job control (
jobs,fg,bg) and& - globbing (
*,?,[...]) - command substitution (
$(...), backticks) - running a script file: the shell rejects arguments
git clone https://github.com/EpicYamo/minishell.git
cd minishell
makeRequires a C compiler and GNU readline:
sudo apt install libreadline-dev./minishell🜁 Y-Shell> echo "hello" | tr a-z A-Z
HELLO
🜁 Y-Shell> cat < Makefile | grep SRC
SRC := minishell.c \
🜁 Y-Shell> export NAME=world
🜁 Y-Shell> echo "hello $NAME"
hello world
🜁 Y-Shell> echo $?
0
🜁 Y-Shell> cat << EOF | wc -l
> one
> two
> EOF
2
Quotes behave as expected: single quotes keep everything literal, double quotes expand variables but keep spaces together.
readline ──► lexer ──► parser ──► executor ──► processes / builtins
│ │ │
│ │ └─ pipes, redirections, wait, $?
│ └─ t_command list, heredocs
└─ tokens: words, quotes, operators, $ expansions
| Directory | Responsibility |
|---|---|
minishell.c |
prompt, readline loop, signal setup, top-level flow |
lexer/ |
splits the line into tokens, handles quotes and $ |
parser/ |
builds t_command nodes, redirections, heredocs, environment parsing |
executor/ |
forks, connects pipes, applies redirections, runs builtins, waits |
built_ins/ |
echo, cd, pwd, export, unset, env, exit |
utils/ |
garbage collector, environment list, signals, banner, helpers |
Two details worth knowing when reading the code:
- Placeholder tokens.
$?and a lone$are replaced by unique sentinel strings before tokenising and turned back into values afterwards, which keeps the lexer from treating their characters as operators. cdchanges the directory, not the variable. It callschdirandpwdreads the result withgetcwd; the inheritedPWD/OLDPWDvariables are left untouched, which is a known difference from Bash.- Garbage collector. Pointers are registered in
t_gcand released once per input line, so error paths do not have to free everything by hand.
SIGINTat the prompt prints a newline and redraws the prompt, like Bash.SIGINTduring a heredoc removes the temporary file and exits with 130.SIGQUITis ignored in the shell and inherited as default by children.$?is updated after every pipeline, including builtins and failures.
readline keeps allocations alive on purpose, so the repository ships suppressions:
cd ignore_readline_leaks
./run.sh # valgrind, with child processes traced
./run_wo_childproc.sh # valgrind, shell onlyBuilt as part of the 42 school curriculum by Arda Aycan (@EpicYamo).
The subject asks for a shell that behaves like Bash for a defined set of features; anything beyond that list was left out on purpose rather than half-implemented.