A basic Unix shell implementation written in C that supports core shell functionality including command execution, built-in commands, and I/O redirection.
- Interactive Command Prompt: Displays current working directory in the format
@LaxmanGupta(Myshell):<current_directory> >>
- Command Line Parsing: Handles whitespace, arguments, and quoted strings
- Process Creation: Uses
fork()
andexecvp()
for command execution - Process Management: Proper parent-child process handling with
wait()
cd <directory>
: Change current working directory- Handles directory change errors with appropriate error messages
exit
: Gracefully exits the shell with a goodbye messagejobs
: List all active background processes
- Input Redirection (
<
): Redirect stdin from a file- Example:
cat < input.txt
- Example:
- Output Redirection (
>
): Redirect stdout to a file (overwrites)- Example:
ls > output.txt
- Example:
- Proper file descriptor management and error handling
- Command Pipelines (
|
): Chain multiple commands together- Example:
ls -la | grep .txt | wc -l
- Example:
- Multi-stage Pipelines: Support for multiple pipes in a single command
- Example:
cat file.txt | grep pattern | sort | uniq
- Example:
- Proper inter-process communication using Unix pipes
- Background Execution (
&
): Run commands in the background- Example:
sleep 10 &
- Example:
- Job Control: Track and manage background processes
- Job Listing: Use
jobs
command to see active background processes - Automatic notification when background jobs complete
- Ctrl+C (SIGINT): Interrupt foreground processes without killing the shell
- Ctrl+Z (SIGTSTP): Suspend foreground processes
- Child Process Management: Automatic cleanup of zombie processes
- Background processes ignore interrupt signals
- Quote Handling: Supports quoted arguments with spaces
- File Redirection Parsing: Automatically detects and handles
<
and>
- Argument Tokenization: Splits commands into proper argument arrays
- Fork failure detection
- File opening errors for redirection
- Directory change errors
- Command execution failures
- Input validation and edge case handling
fork()
- Process creationexecvp()
- Program executionwait()
/waitpid()
- Process synchronizationchdir()
- Directory changesopen()
,close()
- File operationsgetcwd()
- Current directory retrievalpipe()
- Inter-process communicationdup2()
- File descriptor duplicationsignal()
- Signal handlingkill()
- Send signals to processes
- Dynamic memory allocation with
strdup()
- Proper string handling and null termination
- Buffer overflow protection
make shell
make shell_optimized
make run
@LaxmanGupta(Myshell):/home/user >> ls
@LaxmanGupta(Myshell):/home/user >> pwd
@LaxmanGupta(Myshell):/home/user >> date
@LaxmanGupta(Myshell):/home/user >> cd Documents
@LaxmanGupta(Myshell):/home/user/Documents >> jobs
@LaxmanGupta(Myshell):/home/user/Documents >> exit
@LaxmanGupta(Myshell):/home/user >> ls > filelist.txt
@LaxmanGupta(Myshell):/home/user >> cat < input.txt
@LaxmanGupta(Myshell):/home/user >> grep "pattern" < data.txt > results.txt
@LaxmanGupta(Myshell):/home/user >> ls -la | grep .txt
@LaxmanGupta(Myshell):/home/user >> cat file.txt | grep pattern | sort | uniq
@LaxmanGupta(Myshell):/home/user >> ps aux | grep shell
@LaxmanGupta(Myshell):/home/user >> sleep 10 &
[1] 12345
@LaxmanGupta(Myshell):/home/user >> jobs
Active background jobs:
[1] 12345 sleep 10 &
@LaxmanGupta(Myshell):/home/user >>
[1] Done sleep 10 &
@LaxmanGupta(Myshell):/home/user >> echo "Hello World"
@LaxmanGupta(Myshell):/home/user >> mkdir "My Documents"
shell/
├── src/
│ └── shell.c # Main shell implementation
├── bin/ # Compiled binaries (created after build)
├── notes/ # Documentation and implementation notes
│ ├── Pipes.md
│ ├── Fork.md
│ ├── Exec.md
│ ├── Wait_Dup.md
│ ├── Signal.md
│ └── Makefile.md
├── Makefile # Build configuration
└── README.md # This file
- Environment variable expansion (
$VAR
) - Command history
- Tab completion
- Append redirection (
>>
) - Advanced job control (
fg
,bg
commands)
This repository includes detailed implementation notes:
Laxman Gupta
This shell demonstrates fundamental Unix system programming concepts including process management, file I/O, and system call usage in C.