Skip to content

El-Gibbor/Hsh_shell

Repository files navigation

Screenshot 2023-07-17 140214

hsh — A Custom Unix Shell

A custom Unix-like shell written in C, designed to interpret and execute user commands in both interactive and non-interactive modes, and the shell replicates core functionality of /bin/sh, demonstrating proficiency in process management, file I/O, memory handling, and environment manipulation.

It's a real-world simulation of how a minimal shell works under the hood. Developed as part of a systems-level programming sprint at ALX Africa

Key Concepts Demonstrated

Through this implementation, we explored and applied the following systems programming concepts:

  • Architecture and behavior of traditional Unix-based shells.
  • The role of process IDs (PID) and parent processes (PPID).
  • Execution of external programs using execve.
  • Handling environment variables with setenv, unsetenv, and PATH resolution.
  • Differentiating between system calls and C standard library functions.
  • Building a responsive prompt that supports both interactive and piped commands.
  • Graceful handling of signals and end-of-file (EOF) input.

Features

hsh currently supports:

  • ✅ Interactive shell prompt and non-interactive mode (via piping or file input).
  • ✅ Execution of commands with or without arguments.
  • ✅ Environment variable resolution using the PATH.
  • ✅ Built-in commands: exit, setenv, and unsetenv.
  • ✅ Basic signal handling (e.g., Ctrl+D, Ctrl+C).

Built-in Commands:

  • exit – Close the shell.
  • setenv VARIABLE VALUE – Set or update environment variables.
  • unsetenv VARIABLE – Remove environment variables.

All built-ins print a clear error message to stderr on failure.


Planned Enhancements

The shell is actively evolving. Key improvements in the pipeline include:

  • Built-in cd command with full spec support (cd, cd -, update PWD).
  • Input/output redirection (<, >) and pipelines (|).
  • Support for command chaining using ;, e.g., ls /dir1; ls /dir2.
  • Command history and basic autocomplete.

Sample Usage

Interactive Mode:

elgibbor@ubuntu~$ ./hsh
($) /bin/ls
file1  hsh  main.c  shell.c
($) exit

Non Interactive Mode:

elgibbor@ubuntu~$ echo "/bin/ls" | ./hsh
file1  hsh  main.c  shell.c
elgibbor@ubuntu~$ cat file1
/bin/pwd
elgibbor@ubuntu~$ cat file1 | ./hsh
/home/elgibbor

Output Consistency

hsh mirrors /bin/sh in output and error messaging. The only difference is the name of the executable shown in error messages (i.e., ./hsh vs /bin/sh):

/bin/sh error:

$ echo "qwerty" | /bin/sh
/bin/sh: 1: qwerty: not found

hsh error:

$ echo "qwerty" | ./hsh
./hsh: 1: qwerty: not found

Contributors

Built with passion and frustration by:

We welcome feedback and collaboration. Feel free to fork, open issues, or submit pull requests!

About

Implementing a simple UNIX command interpreter.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages