MiniShell is a simple Unix shell implemented in C. This project is designed to help you understand operating system concepts such as process management, signal handling, and command execution. It also demonstrates how to implement features like pipelines, redirection, and background processing.
-
REPL Loop (Read-Evaluate-Print Loop):
Continuously reads user input, evaluates commands, and prints the output with a custom prompt. -
Advanced Parsing:
- Tokenizes input by spaces.
- Supports quoted strings (both single and double quotes) and escape sequences.
-
Built-in Commands:
cd: Changes the current working directory.exit: Exits the shell.
-
External Command Execution:
Usesfork()andexecvp()to execute external commands. -
Pipeline Support:
Supports the|operator to chain commands—allowing the output of one command to become the input of the next.
Example:ls -l | grep ".c" -
Output Redirection:
Supports the>operator to redirect the output of a command to a file.
Example:cat > output.txt -
Background Processing:
Appending&to a command runs it in the background. -
Signal Handling:
- SIGINT: Captures Ctrl+C to prevent the shell from terminating unexpectedly.
- SIGCHLD: Cleans up terminated background processes to avoid zombie processes.
.
├── README.md
├── Makefile
├── main.c
├── parser.c
├── parser.h
├── builtins.c
├── builtins.h
├── executor.c
├── executor.h
├── signals.c
└── signals.h
To compile the project, open a terminal in the project directory and run:
makeThis command uses gcc with appropriate flags to compile all the source files and produces an executable named minishell.
Start the shell by running:
./minishellOnce started, you will see a prompt like myshell>. You can now enter commands as you would in a regular Unix shell.
-
Simple Command Execution:
myshell> ls -l -
Pipeline Execution:
Chain commands using the pipe operator:
myshell> ls -l | grep ".c"
In this example, the output of
ls -lis passed directly as input togrep, filtering the results to show only files with a.cextension. -
Output Redirection:
Redirect command output to a file:
myshell> cat > output.txt
After running the command, type your input and press
CTRL+Dto save it tooutput.txt. -
Background Processing:
Run a command in the background:
myshell> sleep 10 &
This will execute
sleep 10in the background and immediately return the prompt.
You can verify the functionality of MiniShell by testing:
-
Pipeline Test:
Run:ls -l | grep ".c"Verify that only files with a
.cextension are displayed. -
Output Redirection Test:
Run:cat > output.txtType some input, press
CTRL+Dto finish, then check the file content with:cat output.txt -
Background Processing Test:
Run:sleep 10 &Ensure that the shell immediately returns a prompt and displays the background process PID.
- Input Redirection: Add support for the
<operator to redirect input from a file. - Append Redirection: Implement the
>>operator to append output to an existing file. - Remote Command Execution: Use socket programming to allow remote command execution.
- Additional Built-in Commands & Command History: Expand built-in functionalities and add a history feature.
- Improved Error Handling & Logging: Enhance error detection, reporting, and logging mechanisms.
This project is developed for educational purposes. You are free to use, modify, and distribute it as needed.
Thanks to various online resources and documentation on Unix system programming and shell implementation, which have been invaluable in developing this project.