A minimalist UNIX command line interpreter implemented in C.
This shell project is a streamlined implementation of a command-line interpreter that provides essential shell functionality. It handles both interactive and non-interactive modes, command execution, PATH resolution, and basic built-in commands.
- Interactive command prompt (
$
) - Command execution with arguments
- PATH environment variable handling
- Built-in commands:
exit
andenv
- Error handling with appropriate exit statuses
- Memory management
- Non-interactive mode support (commands from pipes)
graph TD
A[Start] --> B{Interactive Mode?}
B -->|Yes| C[Display Prompt]
B -->|No| D[Read Command]
C --> E[Read Command]
E --> F[Parse Command]
D --> F
F --> G{Built-in Command?}
G -->|Yes| H[Execute Built-in]
G -->|No| I{Contains Path?}
I -->|Yes| J[Check if Executable]
I -->|No| K[Search in PATH]
J --> L{Executable?}
K --> L
L -->|Yes| M[Fork Process]
L -->|No| N[Print Error]
M --> O[Execute Command]
O --> P[Wait for Child]
H --> Q{Exit Command?}
P --> Q
N --> Q
Q -->|Yes| R[Exit Shell]
Q -->|No| B
-
Input Reading:
- Interactive mode: Display prompt and read user input
- Non-interactive mode: Read from pipe/file
-
Command Parsing:
- Remove leading/trailing spaces
- Split command into arguments
- Handle special characters
-
Command Execution:
- Check for built-in commands
- Search in PATH if no explicit path
- Fork and execute command
- Handle errors and return status
-
exit [status]
:- Exit the shell with optional status
- Default status: 0
- Handle illegal status numbers
-
env
:- Print current environment variables
- No arguments supported
- Command not found: Exit status 127
- Permission denied: Appropriate error message
- Memory allocation failures: Proper cleanup
- Fork failures: Error message and continue
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh
$ ./hsh
$ ls
hsh main.c shell.h
$ pwd
/home/user/simple_shell
$ exit
$
$ echo "ls" | ./hsh
hsh main.c shell.h
$ cat test_script | ./hsh
[script output]
- Tested on Ubuntu 20.04 LTS
- Compiled with gcc 9.4.0
- Follows Betty style guidelines
main.c
: Main shell loop and input handlingexecute.c
: Command execution functionspath.c
: PATH handling and command locationbuiltins.c
: Built-in command implementationsshell.h
: Header file with prototypes and structures
Edwin : https://github.com/neodwin
Ewan : https://github.com/DARcodertech
Frederic : https://github.com/FredBourouliou