~# Minishell
Custom implementation of a terminal (a small Unix shell) with emphasis on parsing and execution.
This repository contains a compact shell written in C that implements command parsing, execution, pipes, I/O redirections, environment handling and common builtin commands. It is intended as a learning / educational project and a lightweight shell replacement for simple command-line tasks.
- Tokenization and parsing of command lines with:
- single and double quotes
- environment variable expansion (
$VAR) - handling of whitespace
- Execution of:
- external binaries (via
execve) - pipeline chains (using pipes)
- I/O redirections:
>,>>,< - here-document (
<<) support (if implemented)
- external binaries (via
- Basic job of a shell environment:
- maintains a process environment
- builtin commands for shell management
- Signal handling for interactive mode (e.g., Ctrl-C behavior)
The implementation focuses on parsing correctness and the execution flow (fork/exec, file descriptor management).
- POSIX-compatible system (Linux or macOS)
- GCC or clang (C compiler)
- make (Makefile provided)
Clone the repository and run make:
git clone https://github.com/oishchen42/Minishell.git
cd Minishell
makeThis will produce an executable (commonly named minishell — check the Makefile for the exact target).
To clean build artifacts:
make cleanStart the shell:
./minishellYou will see a prompt (depending on implementation). Type commands as you would in a normal shell:
- Run programs:
ls -la - Use pipes:
cat file.txt | grep foo | wc -l - Redirect output:
echo hello > out.txt - Append output:
echo world >> out.txt - Redirect input:
wc -l < file.txt - Combine pipes and redirects:
grep pattern file | sort > result.txt
Exit the shell with the exit builtin or Ctrl-D (EOF) if supported.
-
Run a single command:
/bin/ls -la -
Pipelines:
ps aux | grep minishell | awk '{print $2}' -
Redirections:
echo "hello" > hello.txt cat < hello.txt -
Variable expansion:
echo $HOME -
Quoted strings:
echo "a string with spaces" echo 'literal $HOME is not expanded in single quotes'
Common builtin commands implemented in this project typically include:
cd— change current directoryecho— print textpwd— print working directoryexport— set environment variablesunset— unset environment variablesenv— print environmentexit— exit the shell
Check the source for the exact set and behavior of builtins.
- Parsing: tokens are split respecting quotes and expansions; the parser builds an execution plan (commands, args, redirections, and pipe relationships).
- Execution: the shell forks child processes and uses execve to run external commands. File descriptors for redirections and pipes are set up before exec.
- Signals: the shell should handle interactive signals (SIGINT, SIGQUIT) appropriately — e.g., allow interrupting running child processes without exiting the shell itself.
- Exit codes: the shell should propagate the exit status of the last foreground command.