A UNIX command interpreter written in C.
This project is a lightweight shell inspired by sh behavior and educational shell projects such as minishell. It supports command execution, builtins, PATH resolution, environment management, command chaining, aliases, variable expansion, and persistent history.
- About
- Features
- Supported Builtins
- Project Structure
- How It Works
- Build
- Usage
- Examples
- Limitations
- Authors
hsh runs in:
- Interactive mode (prompt shown as
$). - Non-interactive mode (commands from a file or pipe).
At startup, it loads environment variables into an internal linked list and restores history from ~/.simple_shell_history.
- Execute external commands via
fork()+execve(). - Search executables in
PATH. - Handle script input:
./hsh script_file. - Builtins for shell control and environment management.
- Command chaining with:
cmd1 ; cmd2cmd1 && cmd2cmd1 || cmd2
- Alias creation and lookup (
alias name='value'). - Variable replacement:
$?(last exit status)$$(current process id)$NAME(environment variable)
- Basic comment handling (
#begins a comment when preceded by start/space). - Persistent history written on shell exit.
exit [status]envsetenv NAME VALUEunsetenv NAMEcd [dir]historyalias [name[=value] ...]help(placeholder output in current implementation)
The repository has been organized with top-level folders for growth and maintainability:
.
├── assets/ # Static assets (images, diagrams)
├── docs/ # Extra documentation
├── include/ # Header files
├── src/ # C source files
├── Makefile # Build automation
└── README.md
High-level command flow:
- Read a line from input.
- Remove comments and store history entry.
- Split chained commands (
;,&&,||). - Tokenize into argv.
- Resolve aliases and variables.
- Execute builtin or resolve command path.
- Fork and execute external command.
- Collect status and continue loop.
Core files:
src/main.c: entry point, script file handling.src/shell_loop.c: main loop, builtin dispatch, command execution.src/parser.c: command detection and PATH resolution.src/getLine.c: custom line reader and chain buffer logic.src/vars.c: chaining checks, alias/variable replacement.src/history.c: history read/write and numbering.src/environ.c,src/getenv.c: environment list and env operations.
Compile with make:
makeEquivalent manual compile command:
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 -Iinclude src/*.c -o hshClean build artifacts:
make cleanRun interactive shell:
./hshRun a script file:
./hsh path/to/script.shRun non-interactively from pipe:
echo "env" | ./hshCommand chaining:
ls -la ; pwdConditional execution:
mkdir test && cd test || echo "mkdir failed"Alias:
alias ll='ls -la'
llVariables:
echo $$
echo $?
echo $HOME- No full quote parsing engine (single/double quote semantics are limited).
- No pipes (
|) between processes in the current implementation. - No redirections (
>,<,>>) in the current implementation. helpbuiltin is not fully implemented.
See repository contributors for the current author list.