arsh is a robust, custom implementation of a Unix shell, written from scratch in C. This project demonstrates a deep understanding of system programming concepts, including process management, signal handling, and inter-process communication.
- Interactive REPL: A continuous Read-Eval-Print Loop that accepts and executes user commands.
- Custom Prompt: informative prompt displaying user, hostname, and current working directory.
- Command Execution: Seamless execution of external programs using
forkandexecvp. - Built-in Commands:
cd: Change the current working directory.help: Display information about the shell.exit: Terminate the shell session.export: Set environment variables (e.g.,KEY=VALUE).unset: Remove environment variables.
- I/O Redirection:
>: Redirect standard output to a file (overwrite).>>: Redirect standard output to a file (append).<: Redirect standard input from a file.
- Piping: Chain commands using
|to pass output from one process as input to another. - Logical Operators:
&&: Execute the following command only if the previous one succeeds.||: Execute the following command only if the previous one fails.
- Wildcard Expansion: Globbing support for
*and?patterns. - Environment Variables:
- Expand variables using
$VAR. - Access exit status of the last command with
$?.
- Expand variables using
- Job Control:
- Run commands in the background with
&. - Automatic reaping of zombie processes.
- Run commands in the background with
- Signal Handling: Graceful handling of signals like
SIGINT(Ctrl+C). - Script Execution: Ability to run commands from a script file provided as an argument.
- Line Editing & History:
- Navigate command history with Up/Down arrow keys.
- Edit the current line using Left/Right arrows, Home, End, and Backspace.
The codebase is organized for modularity and maintainability:
.
├── include/ # Header files defining interfaces
│ ├── builtins.h
│ ├── executor.h
│ ├── input.h
│ ├── parser.h
│ └── shell.h
├── src/ # Source code implementations
│ ├── builtins.c # Built-in command logic
│ ├── executor.c # Process creation and execution
│ ├── input.c # Input reading and history management
│ ├── main.c # Entry point and main loop
│ └── parser.c # Command parsing and tokenization
├── Makefile # Build configuration
└── README.md # Project documentation
For detailed installation instructions for Linux, macOS, and Windows (via WSL), please refer to INSTALLATION.md.
To compile the project, run:
makeTo remove build artifacts:
make cleanStart the shell interactively:
./arshRunning commands:
arsh> ls -la
arsh> echo "Hello World"Piping and Redirection:
arsh> ls | grep ".c" > source_files.txtBackground Jobs:
arsh> sleep 10 &Running Scripts: You can also execute commands from a file:
./arsh script.txtThe shell operates through a structured lifecycle:
- Initialization: Sets up signal handlers and environment.
- Read: Captures user input or reads from a file.
- Parse: Tokenizes the input, handling quotes and special characters.
- Expand: Processes environment variables and wildcard patterns.
- Execute:
- Identifies and runs built-in commands directly.
- Manages pipelines and redirections.
- Forks child processes for external commands.
- Waits for foreground processes to complete.
This project is licensed under the MIT License - see the LICENSE file for details.