This project is a lightweight, custom shell implementation written in C. It demonstrates the core mechanics of a command-line interface, including process management, environment variable handling, and command dispatching via a lookup table.
Made this while exploring how fork() and exec() work, and this is done with 0 AI. (except this readme)
- Built-in Commands: Native support for common operations (
cd,pwd,echo,type,exit). - System Integration: Automatically searches the system
PATHto execute external binaries. - Process Management: Uses
fork,execvp, andwaitto handle external program execution safely. - Lookup Table Architecture: Uses a function-pointer-based dispatch system, making it trivial to add new built-in commands.
echo [args]: Prints text to the standard output.pwd: Displays the current absolute working directory.cd [path]: Changes the current directory. Supports~for the home directory.type [command]: Identifies if a command is a shell built-in or an external binary (providing the file path).exit: Terminate the shell session.
Any command not found in the built-in lookup table (e.g., ls, grep, cat, gcc) is searched for in the directories listed in your environment's PATH. If found, the shell spawns a child process to execute it.
The shell operates on a standard REPL (Read-Eval-Print Loop) cycle:
- Read: Captures user input via
fgetsand strips the newline. - Eval:
- Tokenizes the string into an argument array.
- Checks the
LookupTablefor a matching internal function. - If no match is found, it forks a new process to execute the command as an external program.
- Print: Output is directed to
stdout. - Loop: Returns to the prompt until
exitis called.
- Standard C Library (
stdio,stdlib,string) - POSIX API (
unistd,sys/wait)
The shell dynamically allocates memory for command strings to handle varying input lengths and ensures all allocated memory is freed before the next prompt, preventing leaks during long-running sessions.
Use any standard C compiler (like gcc or clang):
gcc -o cshell main.c
./cshell
Once inside, you will see the $ prompt. You can then run commands like:
$ echo hello world
$ pwd
$ type ls
$ cd /tmp
- Support for I/O redirection (
>and<). - Command piping (
|). - Background process execution (
&). - Support for quoted strings (e.g.,
echo "hello world"as a single argument).