A minimal Linux shell written in C that reads commands from a prompt, executes them using system calls, supports pipes, handles signals, and manages background jobs — just like a real terminal.
User types a command
↓
get_command() ← extract command name
↓
check_command_type() ← BUILTIN / EXTERNAL / NO_COMMAND
↓ ↓
execute_internal() fork() + execvp()
- 💬 Custom prompt — change anytime with
PS1=newprompt - 🔧 Built-in commands —
cd,pwd,echo,exit,jobs,fg,bg - ⚡ External commands — 150+ Linux commands via
fork()+execvp() - 🔗 Pipes — chain commands using
| - 📡 Signal handling —
Ctrl+CandCtrl+Zhandled gracefully - 🌙 Job control — suspend, list, and resume background processes
Minishell/
├── main.c # Entry point — initializes prompt and starts the shell
├── scan_input.c # Shell loop — reads input, routes commands, handles signals
├── commands.c # Executes built-in and external commands, handles pipes
├── header.h # Structs, macros, and all function declarations
└── ext_cmds.txt # List of supported external commands (loaded at startup)
- GCC compiler
- Linux / Unix environment
gcc main.c scan_input.c commands.c -o minishell./minishellminishell$: pwd
/home/amar
minishell$: cd /tmp
minishell$: ls | grep .c
main.c
scan_input.c
commands.cminishell$: echo $$ # shell PID
minishell$: echo $? # last exit status
minishell$: echo $SHELL # shell pathminishell$: sleep 100 # press Ctrl+Z to suspend
minishell$: jobs
[1] Stopped sleep 100
minishell$: fg # resume in foreground
minishell$: bg # resume in backgroundminishell$: PS1=dev>
dev>| Signal | Shortcut | What happens |
|---|---|---|
SIGINT |
Ctrl+C |
Cancels current command, re-shows prompt |
SIGTSTP |
Ctrl+Z |
Suspends process, adds it to jobs list |
SIGCHLD |
auto | Cleans up finished background processes |
| Concept | Purpose |
|---|---|
fork() + execvp() |
Run external commands in a child process |
pipe() + dup2() |
Connect stdout of one command to stdin of the next |
waitpid() |
Wait for child; detect stopped jobs |
signal() + kill() |
Handle Ctrl+C, Ctrl+Z and resume jobs |
getcwd() + chdir() |
Implement pwd and cd |
File I/O (open, read) |
Load ext_cmds.txt at startup |
Amar C M — Embedded Systems Engineer
MIT License — free to use and modify with attribution.