A custom Unix shell implementation written in C that provides basic shell functionality including command execution, built-in commands, path management, I/O redirection, and parallel command execution.
cd <directory>- Change current working directorypath [directories...]- Set executable search pathsexit- Exit the shell (must be called without arguments)
- I/O Redirection - Redirect command output to files using
> - Parallel Commands - Execute multiple commands concurrently using
&separator - Environment Variables - Support for environment variable expansion using
$VARIABLE - Quoted Strings - Handle arguments with spaces using double quotes
- Batch Mode - Execute commands from script files
- Interactive Mode - Standard shell prompt interface
gcc -o witsshell witsshell.cRun the shell interactively:
./witsshellYou'll see the witsshell> prompt where you can enter commands.
Execute commands from a file:
./witsshell script.txtwitsshell> ls -la
witsshell> cat file.txt
witsshell> pwd# Change directory
witsshell> cd /home/user
# Set executable paths
witsshell> path /bin /usr/bin /usr/local/bin
# Clear all paths
witsshell> path
# Exit shell
witsshell> exit# Redirect output to file
witsshell> ls -la > output.txt
witsshell> echo "Hello World" > greeting.txt# Use environment variables
witsshell> echo $HOME
witsshell> ls $HOME# Handle arguments with spaces
witsshell> echo "This is a quoted string"
witsshell> mkdir "My Documents"# Execute commands in parallel
witsshell> sleep 5 & echo "Hello" & ls- Default path is set to
/bin/on startup - Use the
pathcommand to modify executable search paths - Commands are searched in the order paths were specified
- If no paths are set, external commands cannot be executed
- All errors output the standard message: "An error has occurred\n"
- Errors are written to stderr
- Invalid commands, file operations, and syntax errors are handled gracefully
- External commands are executed in child processes using
fork()andexecv() - Parent process waits for child completion unless running in parallel
- Proper cleanup of zombie processes in parallel execution
- Dynamic memory allocation for paths and command arguments
- Proper cleanup of allocated memory on exit
- Handles memory allocation failures gracefully
- Only supports output redirection (
>) - Redirects both stdout and stderr to the specified file
- File is created with permissions 0644
- Multiple
>symbols in a command result in an error - Redirection must have a valid filename
- Commands separated by
&run concurrently - Each command runs in its own child process
- Shell waits for all parallel commands to complete before accepting new input
- Variables prefixed with
$are expanded to their values - Undefined variables are treated as empty strings
- Expansion occurs during command tokenization
The shell will output "An error has occurred\n" for:
- Invalid file operations
- Command not found
- Memory allocation failures
- Invalid syntax (malformed redirection, etc.)
- Built-in command errors (invalid directory for
cd, etc.) - Exit command called with arguments
- No support for input redirection (
<) - No piping between commands
- No background process management beyond parallel execution
- No command history or tab completion
- No support for complex quoting or escaping beyond basic double quotes
- No globbing or wildcard expansion
witsshell.c # Main source file containing all implementation
README.md # This documentation file
This project was created as an educational exercise. Feel free to use and modify as needed.
Created as part of a systems programming exercise to understand Unix shell internals and process management.