A simple shell implemented in C. Supports running commands in foreground and background, handling signals, basic I/O redirection, and built-in commands.
- Execute system commands using
execvp() - Built-in commands:
exit: Terminates the shell and kills any remaining background processes.cd [path]: Changes the current working directory. If no path is given, goes to$HOME.status: Reports the exit status or termination signal of the last foreground process.
- Input/output redirection:
command < input.txt > output.txt - Background execution with
& - Foreground-only mode toggled with
Ctrl+Z(SIGTSTP) $$is replaced with the shell’s PID (useful for creating unique filenames)- Suggestion system: suggests similar built-in commands for unrecognized input (e.g.,
ll->ls)
gcc -o tinyshell tinyshell.c./tinyshell| Command | Description | Example | Expected Output |
|---|---|---|---|
exit |
Exits the shell | exit |
Shell terminates |
cd |
Changes to home directory | cd |
No output, current dir is $HOME |
cd [path] |
Changes to specified directory | cd /tmp |
No output, current dir is /tmp |
status |
Shows exit status of last foreground cmd | status |
exit value 0 or signal msg |
| Feature | Example | Expected Output |
|---|---|---|
| Run command | ls |
Lists current directory contents |
| Run in background | sleep 5 & |
background pid is [PID] |
| Redirect input | sort < input.txt |
Sorts lines from input.txt |
| Redirect output | ls > out.txt |
Output of ls saved in out.txt |
| Redirect both | sort < in.txt > out.txt |
Sorted in.txt saved to out.txt |
| Variable expansion | echo mypid_$$ |
Prints mypid_[shell_pid] |
| Foreground-only toggle | Press Ctrl+Z |
Toggles fg-only mode and shows message |
| Background process done | After sleep 2 & |
After 2s: background pid [PID] is done: |
- Foreground-only mode disables
&from launching background tasks. - Background process completion is reported when done.
- Handles
SIGINT(Ctrl+C) for foreground processes only.
-
Install WSL
- Open PowerShell and run:
wsl --install - Restart your computer if prompted.
- Open PowerShell and run:
-
Set Up Ubuntu
- Ubuntu will install automatically. If not, install it from the Microsoft Store.
-
Install VSCode & WSL Extension
- Open VSCode and install the Remote - WSL extension.
- Press
F1, then chooseRemote-WSL: New Window.
-
Open Your Project in WSL
- Use the new WSL window to open your project folder.
-
Compile & Run the Shell
gcc -o tinyshell tinyshell.c ./tinyshell
- Install MinGW or Cygwin.
- Open a terminal in your install environment and try:
gcc -o tinyshell tinyshell.c ./tinyshell
- Note: Some system headers and signals may not work as expected in native Windows environments.
-
The command suggestion feature uses
strncasecmp, which is declared in the header:#include <strings.h>
Make sure this is included if you're compiling on Linux
-
Command suggestion compares the first 2 letters of your input with known built-ins like
cd,exit, etc. You can customize this list insidesuggestCommand()