A simple shell implementation written in C.
Wish (Wisconsin Shell) is a lightweight command-line interface that provides basic shell functionality. This implementation offers essential shell capabilities with a clean, minimalist interface.
- Command execution with path searching
- Built-in commands:
cd [directory]
- Change directoryexit
- Exit the shellpath [directory1] [directory2] ...
- Set search path for executables
- I/O redirection with
>
operator - Parallel command execution with
&
operator - Support for both interactive and batch modes
- Error handling with standardized error messages
- File I/O redirection (for batch mode)
- GCC or another C compiler
- Make (for build automation)
To build the shell using the provided Makefile:
make
Or you can still build manually:
gcc -o wish wish.c
The project includes a Makefile with several useful targets:
make
ormake all
- Builds the wish shell executablemake clean
- Removes compiled files (the executable and object files)make debug
- Builds with debug symbols for debugging with tools like gdb
Example:
# Compile with debug symbols
make debug
# Clean up compiled files
make clean
Run in interactive mode:
./wish
Run in batch mode (provide a batch file):
./wish batch_file
You can also specify an output file:
./wish batch_file output_file
When launched without arguments, the shell runs in interactive mode:
- The prompt
wish>
appears, waiting for your commands - Enter commands like you would in any shell
- Use built-in commands (
cd
,exit
,path
) or any system commands
Pass a file path as an argument to run commands from that file:
- One command per line
- No prompt is displayed
- All output goes to stdout (or specified output file)
The shell maintains a list of directories to search for executable files:
- Default search path includes
/bin
and/usr/bin
- You can modify the search path using the
path
command path
with no arguments clears the search path- Examples:
path /usr/local/bin /bin /usr/bin
- Sets search path to these three directoriespath
- Clears all search paths (you won't be able to execute any commands afterwards)
The shell supports redirecting command output to files:
- Use the
>
operator followed by a filename - Example:
ls > output.txt
- Redirects the output ofls
to output.txt - Errors are properly handled:
- No filename provided:
ls >
will produce an error - Multiple redirection operators:
ls > file1 > file2
will produce an error - Redirection at the start:
> file
will produce an error
- No filename provided:
The shell supports running multiple commands in parallel:
- Use the
&
operator to separate commands - Example:
ls & pwd & echo hello
- Runs all three commands in parallel - The shell waits for all parallel commands to complete before accepting new input
- Parallel commands can be combined with redirection
- Example:
ls > file1 & pwd > file2
- Redirects output of parallel commands to different files
- Example:
- You can run up to 16 commands in parallel
The WISH shell is implemented in wish.c
with the following key components:
- Main Shell Loop: Processes input commands in
wish_shell()
- Command Execution: Handles both built-in and external commands
- Redirection Handling: Parses and processes output redirection
- Path Management: Manages the search path for executable files
- Error Handling: Consistent error reporting throughout the shell
The codebase contains comprehensive comments that explain:
- Purpose and functionality of each function
- Data structures and algorithms used
- Error handling strategies
- Command execution flow
- Complex parsing logic, especially for redirection
This project is open source and available under the MIT License.