A lightweight, feature-rich command shell implementation in C that mimics the behavior of traditional Unix shells like bash. This project demonstrates core shell concepts including command parsing, process management, piping, redirection, and built-in commands.
- Interactive Mode: Full interactive shell with command history and line editing (powered by GNU Readline)
- Non-Interactive Mode: Execute commands from scripts or piped input
- Piping: Connect multiple commands using pipes (
|) to chain operations - Input/Output Redirection:
- Input redirection with
<operator - Output redirection with
>operator
- Input redirection with
- Background Process Execution: Run commands in the background using
&operator - Environment Variables: Set and use environment variables within the shell
- Built-in Commands: Support for shell built-in commands like
cd,exit, andhelp - Signal Handling: Graceful handling of SIGINT (Ctrl+C) for process management
- Command History: Interactive mode maintains command history for easy recall
- Debug Mode: Compile with debugging symbols to trace token parsing and command execution
The project supports multiple compilation modes via the Makefile:
make mainCompiles the shell optimized for production use with readline support.
- Output:
./mainexecutable - Compilation Command:
gcc -o main main.c shell.c parser.c -lreadline
make debugCompiles with debugging symbols and enables debug output tracing.
- Output:
./mainexecutable with debug information - Compilation Command:
gcc -o main main.c shell.c parser.c debug.c -DDEBUG_SHELL -lreadline -g - Features: Shows token parsing and command structure during execution
make testRuns comprehensive tests comparing shell output with bash output.
- Process:
- Executes commands from
test.txtthrough mini_shell and generatesmini_shell.txt - Executes the same commands through bash and generates
bash.txt - Compares outputs using
diffto ensure compatibility
- Executes commands from
make cleanRemoves compiled executable and generated test files.
Simply execute the shell:
./mainThe prompt will display the current working directory, e.g., [/home/user]>
Execute commands from a file or pipe:
cat commands.txt | ./main
./main < commands.txt
echo "ls -la" | ./main[/home]>ls -la
[/home]>pwd
[/home]>echo "Hello World"[/home]>cat file.txt | grep "search_term" | wc -l[/home]>ls -la > output.txt
[/home]>echo "data" > file.txt[/home]>sort < unsorted.txt[/home]>sleep 100 &
[/home]>./long_running_process &[/home]>MY_VAR = "Hello"
[/home]>printenv MY_VAR[/home]>cd /tmp
[/tmp]>cd ..The mini shell provides the following built-in commands:
- Usage:
exit - Description: Terminates the shell session with message "Bye!"
- Example:
[/home]>exit Bye!
- Usage:
cd [directory] - Description: Changes the current working directory
- Arguments:
directory: Path to change to (relative or absolute)- If no argument provided: Attempts to change directory
- Example:
[/home]>cd /tmp [/tmp]>cd .. [/]>
- Usage:
help - Description: Displays information about all available built-in commands
- Example:
[/home]>help Builtins 1.Exit: To exit the shell 2.cd[dir]: To change current directory 3.help: to print this message
- main.c: Entry point handling interactive and non-interactive modes
- shell.c: Process execution, piping, redirection, and built-in command implementation
- parser.c: Tokenization and command parsing logic
- shell.h: Header file with data structures and function declarations
- debug.c: Debug output functions for development
- Token: Represents parsed tokens with type information (COMMAND, ARGUMENT, PIPE, etc.)
- Command: Represents a parsed command with arguments, input/output files, and job type
- E_table: Environment variable table for storing variable assignments
make testThe test target:
- Executes commands from
test.txtusing mini_shell and pipes output tomini_shell.txt - Executes the same commands using bash and pipes output to
bash.txt - Compares both outputs using
diffto verify compatibility
0 files differ (no differences found)
- GCC: GNU C Compiler
- Readline Library: For interactive line editing (
libreadline-devon Ubuntu/Debian) - Standard POSIX Utilities: fork, exec, pipe, dup2, signal handling
sudo apt-get install build-essential libreadline-dev- Support for more advanced redirections (e.g.,
>>,2>,&>) - Job control and foreground/background job management
- Command substitution and glob patterns
- Additional built-in commands (
echo,export,set, etc.) - Alias support
- Script file execution
This is an educational project demonstrating shell implementation concepts.