A simple UNIX command line interpreter (shell) built in C language as part of the ALX SE Program.
- Features
- Installation
- Usage
- Built-in Commands
- External Commands
- Advanced Features
- Examples
- Project Structure
- Authors
- Interactive and Non-interactive modes
- Command execution with arguments
- PATH environment variable resolution
- Built-in command support
- Signal handling (Ctrl+C)
- Error handling with proper exit codes
- Memory management and cleanup
- Current working directory display in prompt
- Smart directory navigation
- Environment variable support
- Command history tracking
- GCC compiler
- Linux/Unix environment
- Standard C library
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh./hshLaunch the shell and interact with it directly:
$ ./hsh
/home/user/simple_shell$ ls -la
/home/user/simple_shell$ cd /tmp
/tmp$ pwd
/tmp$ exitExecute commands via pipes or input redirection:
$ echo "ls -la" | ./hsh
$ echo -e "pwd\ncd ..\npwd" | ./hshNavigate between directories with full support for:
cd- Go to HOME directorycd [directory]- Change to specified directorycd ..- Go to parent directorycd -- Return to previous directory
Examples:
/home/user$ cd /tmp
/tmp$ cd ..
/$ cd -
/tmp$ cd
/home/user$Display all environment variables:
/home/user$ env
PWD=/home/user
PATH=/usr/local/bin:/usr/bin:/bin
HOME=/home/user
USER=user
...Exit the shell with optional status code:
/home/user$ exit
$ echo $?
0The shell supports all standard UNIX commands available in your PATH:
ls- List directory contentspwd- Print working directorymkdir- Create directoriesrmdir- Remove directoriescp- Copy filesmv- Move/rename filesrm- Remove filesfind- Search files and directories
echo- Display textcat- Display file contentsgrep- Search text patternssort- Sort linesuniq- Remove duplicateswc- Count words, lines, characters
ps- Process informationwho- Show logged usersdate- Display date and timeuname- System information
/home/user$ ls -la
/home/user$ echo "Hello, World!"
/home/user$ cat file.txt
/home/user$ ps aux
/home/user$ which ls- Current directory shown in prompt
- **Home directory abbreviated as ~**
- Subdirectories shown relative to home
/home/user/simple_shell$ cd Documents
/home/user/Documents$ cd ..
/home/user$ cd /
/$ cd home/user
/home/user$Automatically finds executables in PATH directories:
$ /bin/ls # Absolute path
$ ls # Found via PATH resolution- PWD - Always updated with current directory
- OLDPWD - Tracks previous directory for
cd - - PATH - Used for command resolution
- HOME - Used for
cdwith no arguments
- Ctrl+C (SIGINT) - Gracefully handled, returns to prompt
- Clean exit - Proper memory cleanup on exit
- Command not found - Clear error messages
- Permission denied - Proper error reporting
- Invalid directory - Helpful error messages
$ invalidcommand
./hsh: 1: invalidcommand: not found
$ cd /invalid/path
./hsh: 1: cd: can't cd to /invalid/path$ ./hsh
/home/user/simple_shell$ pwd
/home/user/simple_shell
/home/user/simple_shell$ ls -la
total 156
drwxr-xr-x 3 user user 4096 Dec 6 14:37 .
drwxr-x--- 5 user user 4096 Dec 6 14:39 ..
-rwxr-xr-x 1 user user 26624 Dec 6 14:37 hsh
.../home/user/simple_shell$ cd /tmp
/tmp$ pwd
/tmp
/tmp$ cd -
/home/user/simple_shell$ cd ..
/home/user$ cd simple_shell
/home/user/simple_shell$/home/user$ env | grep PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
/home/user$ echo $HOME
$HOME/home/user$ echo "Hello World"
Hello World
/home/user$ which ls
/usr/bin/ls
/home/user$ date
Fri Dec 6 14:37:22 UTC 2024simple_shell/
βββ main.h # Header file with function prototypes
βββ main.c # Main shell loop and initialization
βββ prompt.c # Prompt display and directory handling
βββ _fork.c # Process creation and execution
βββ _func1.c # String manipulation functions
βββ _func2.c # Input handling functions
βββ _func3.c # Token parsing functions
βββ _func4.c # Built-in commands implementation
βββ path.c # PATH resolution functions
βββ _env.c # Environment variable functions
βββ _env_non.c # Non-interactive environment handling
βββ non_interactive.c # Non-interactive mode implementation
βββ name_of_prg.c # Program name handling
βββ AUTHORS # Project authors
βββ README.md # This file
- Dynamic memory allocation for command buffers
- Proper cleanup on exit and error conditions
- No memory leaks in normal operation
- Fork-exec model for external commands
- Proper parent-child process synchronization
- Signal handling for interrupts
- Custom string manipulation functions
- Safe buffer handling
- Tokenization and parsing
- No pipe (
|) support - No redirection (
>,<,>>) support - No command substitution
- No variable assignment
- No scripting support (if/while/for)
- No job control (background processes)
See the AUTHORS file for contributor information.