Skip to content

hamza-25/simple_shell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

79 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Simple Shell - HSH

A simple UNIX command line interpreter (shell) built in C language as part of the ALX SE Program.

πŸ“‹ Table of Contents

✨ Features

Core Shell Functionality

  • 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

Enhanced User Experience

  • Current working directory display in prompt
  • Smart directory navigation
  • Environment variable support
  • Command history tracking

πŸ”§ Installation

Prerequisites

  • GCC compiler
  • Linux/Unix environment
  • Standard C library

Compilation

gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o hsh

Run the Shell

./hsh

πŸš€ Usage

Interactive Mode

Launch the shell and interact with it directly:

$ ./hsh
/home/user/simple_shell$ ls -la
/home/user/simple_shell$ cd /tmp
/tmp$ pwd
/tmp$ exit

Non-Interactive Mode

Execute commands via pipes or input redirection:

$ echo "ls -la" | ./hsh
$ echo -e "pwd\ncd ..\npwd" | ./hsh

πŸ”¨ Built-in Commands

cd - Change Directory

Navigate between directories with full support for:

  • cd - Go to HOME directory
  • cd [directory] - Change to specified directory
  • cd .. - Go to parent directory
  • cd - - Return to previous directory

Examples:

/home/user$ cd /tmp
/tmp$ cd ..
/$ cd -
/tmp$ cd
/home/user$

env - Environment Variables

Display all environment variables:

/home/user$ env
PWD=/home/user
PATH=/usr/local/bin:/usr/bin:/bin
HOME=/home/user
USER=user
...

exit - Exit Shell

Exit the shell with optional status code:

/home/user$ exit
$ echo $?
0

πŸ”§ External Commands

The shell supports all standard UNIX commands available in your PATH:

File and Directory Operations

  • ls - List directory contents
  • pwd - Print working directory
  • mkdir - Create directories
  • rmdir - Remove directories
  • cp - Copy files
  • mv - Move/rename files
  • rm - Remove files
  • find - Search files and directories

Text Processing

  • echo - Display text
  • cat - Display file contents
  • grep - Search text patterns
  • sort - Sort lines
  • uniq - Remove duplicates
  • wc - Count words, lines, characters

System Information

  • ps - Process information
  • who - Show logged users
  • date - Display date and time
  • uname - System information

Examples:

/home/user$ ls -la
/home/user$ echo "Hello, World!"
/home/user$ cat file.txt
/home/user$ ps aux
/home/user$ which ls

🎯 Advanced Features

Smart Prompt Display

  • 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$

PATH Resolution

Automatically finds executables in PATH directories:

$ /bin/ls        # Absolute path
$ ls             # Found via PATH resolution

Environment Variable Handling

  • PWD - Always updated with current directory
  • OLDPWD - Tracks previous directory for cd -
  • PATH - Used for command resolution
  • HOME - Used for cd with no arguments

Signal Handling

  • Ctrl+C (SIGINT) - Gracefully handled, returns to prompt
  • Clean exit - Proper memory cleanup on exit

Error Handling

  • 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

πŸ“ Examples

Basic Usage

$ ./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
...

Directory Navigation

/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$

Environment Variables

/home/user$ env | grep PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
/home/user$ echo $HOME
$HOME

Command Execution

/home/user$ echo "Hello World"
Hello World
/home/user$ which ls
/usr/bin/ls
/home/user$ date
Fri Dec  6 14:37:22 UTC 2024

πŸ“ Project Structure

simple_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

πŸ” Technical Details

Memory Management

  • Dynamic memory allocation for command buffers
  • Proper cleanup on exit and error conditions
  • No memory leaks in normal operation

Process Handling

  • Fork-exec model for external commands
  • Proper parent-child process synchronization
  • Signal handling for interrupts

String Processing

  • Custom string manipulation functions
  • Safe buffer handling
  • Tokenization and parsing

🚫 Limitations

  • No pipe (|) support
  • No redirection (>, <, >>) support
  • No command substitution
  • No variable assignment
  • No scripting support (if/while/for)
  • No job control (background processes)

🀝 Authors

See the AUTHORS file for contributor information.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages