Skip to content

abdelrahmann22/simple_shell

Repository files navigation

Simple Shell

A lightweight Unix shell implementation in C that provides essential command-line functionality including built-in commands, program execution, environment variable management, and alias support.

Features

  • Built-in Commands

    • cd [dir] - Change the working directory
    • exit [STATUS] - Exit the shell with optional status code
    • env - Display environment variables
    • setenv VARIABLE VALUE - Set or modify an environment variable
    • unsetenv VARIABLE - Remove an environment variable
    • help [BUILTIN_NAME] - Display help information
    • alias [name='value'] - Create, display, or modify aliases
  • External Program Execution

    • Forks child processes to execute external commands
    • Searches PATH for executables
    • Properly handles exit codes and signals
  • Additional Features

    • Interactive mode with prompt (dali<3 )
    • Non-interactive mode for reading commands from files
    • Signal handling (Ctrl+C)
    • Variable expansion ($VAR)
    • Alias expansion
    • Error reporting and status codes

Building

Compile the shell with gcc:

gcc -o simple_shell *.c

Usage

Interactive Mode

Run the shell and start typing commands:

./simple_shell
dali<3 ls -l
dali<3 cd /home
dali<3 exit

Non-Interactive Mode

Execute commands from a file:

./simple_shell commands.txt

Examples

# Change directory
dali<3 cd /tmp

# List files (external command)
dali<3 ls -la

# Set environment variable
dali<3 setenv MYVAR "hello world"

# Display environment
dali<3 env

# Create an alias
dali<3 alias ll="ls -la"

# Use the alias
dali<3 ll

# Get help
dali<3 help cd
dali<3 help

# Exit with status
dali<3 exit 0

Project Structure

  • main.c - Entry point and main loop
  • exec.c - Command execution using fork/execve
  • builtin.c - Built-in commands (env, setenv, unsetenv, exit, alias)
  • more_builtin.c - Additional built-in commands (cd, help)
  • env_manage.c - Environment variable management
  • alias_manage.c - Alias management
  • tokensize.c - Input tokenization
  • strtok.c - String tokenization
  • _getline.c - Input line reading
  • helpers.c - String utility functions
  • more_helpers.c - Additional helper functions
  • even_more_helpers.c - More helper functions
  • fin_paths.c - Path resolution
  • exp.c - Variable and alias expansion
  • shell.h - Header file with structures and function declarations
  • macro.h - Macros for messages and configuration

Implementation Details

  • Uses fork() and execve() for process creation
  • Maintains its own copy of the environment variables
  • Uses a custom data structure (data_of_program) to track program state
  • Handles signals gracefully (Ctrl+C displays a new prompt)
  • Supports chaining commands via logic operators
  • Implements custom string functions to avoid libc dependencies where possible

License

This project is provided as-is for educational purposes.

About

A lightweight Unix shell implementation in C

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages