Welcome to Minishell 101! This is a beginner-friendly guide and README template for the 42 School minishell project. The goal of this project is to create a simple UNIX shell—a program that takes commands from the user and executes them, much like bash.
Minishell is a journey into process creation, file descriptors, signals, and parsing. By the end of this project, you'll have a deep understanding of how your terminal interprets what you type and how it asks the operating system to do the work.
- Prompt: Displays a prompt when waiting for a new command.
- History: Keeps a working history of commands (using
readline). - Execution: Finds and executes executables using the
PATHvariable or relative/absolute paths. - Built-ins: Recreates 7 essential built-in functions (see below).
- Pipes (
|): Connects the output of one command to the input of the next. - Redirections:
>(Redirect output)<(Redirect input)>>(Redirect output in append mode)<<(Here-document)
- Environment Variables: Expands variables like
$USERor$?(exit status). - Signals: Handles
ctrl-C,ctrl-D, andctrl-\just like bash.
Your shell must natively implement the following commands without calling external binaries:
echo(with-noption)cd(with relative or absolute paths)pwd(print working directory)export(export environment variables)unset(unset environment variables)env(print the environment)exit(exit the shell, handling exit codes)
A well-structured minishell usually follows these four steps:
- Lexer (Tokenization): Breaks the user's input string into small, manageable chunks (tokens) like
WORD,PIPE,REDIRECT_IN, etc. - Parser: Organizes those tokens into a structure (like an Abstract Syntax Tree or a linked list of commands) that the computer can easily understand.
- Expander: Loops through the parsed arguments and replaces any
$VARIABLESwith their actual values from the environment. - Executor: Iterates through the command structures. It sets up pipes and redirections (using
dup2), forks processes (usingfork), and runs the commands (usingexecveor built-in functions).
To compile the project, simply run make in the root directory.
makeStart the shell by running the executable:
./minishellmake clean: Removes the object files.make fclean: Removes object files and the executable.make re: Recompiles the whole project.
- Waitpid & Fork: Understand how parent processes wait for child processes.
- File Descriptors: Grasp how
dup2()is used to wirestdoutof one process into thestdinof another. - Memory Leaks:
valgrindis your best friend. Make sure you free your abstract syntax tree or command lists after every prompt loop! - Bash is your standard: Whenever you are unsure how minishell should behave, type the same command into
bashand mimic its behavior.
Created for the 42 Cursus (yel-mota).