This project is a minimalist Unix shell implemented in C, designed to explore the core mechanics of how operating systems manage processes and execute commands.
The goal of this project is to understand the fundamental lifetime cycle of a shell:
- Initialize: Setting up the environment.
- Interpret: A loop that reads input from
stdin, parses it into commands/arguments, and executes them. - Terminate: Freeing memory and exiting.
This is a learning-focused implementation. While it mimics the behavior of standard shells like Bash, it focuses on essential system calls and logic required for process management.
- Command Loop: A standard REPL (Read-Eval-Print Loop) that provides a prompt and waits for user input.
- Input Parsing: Handles dynamic memory allocation to read lines and tokenize them into individual arguments.
- Process Creation: Utilizes
fork()andexec()system calls to launch programs. - Process Synchronization: Uses
waitpid()to ensure the shell waits for child processes to finish. - Built-in Commands: Includes internal implementations for
cd,help, andexit.
- Memory Management: Manual handling using
malloc,realloc, andfreeto manage command buffers. - System Calls: Direct usage of POSIX APIs, including
unistd.hfor process control andsys/wait.hfor state management. - Tokenization: Uses
strtokto split input strings based on whitespace delimiters.
This project was developed as a hands-on learning exercise to build a foundation for more complex systems. It is currently a work in progress, and I plan to add more advanced features in the future.
Potential enhancements include:
- Implementing support for quotes and backslash escaping in arguments.
- Adding piping (
|) and I/O redirection (<,>). - Implementing signal handling and background process execution.
To compile and run the shell on your local machine, use the following commands in your terminal:
# Compile the source code
gcc main.c shell.c -o myshell
# Run the executable
./myshell