Skip to content

AmarCM/Minishell

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🐚 Minishell — Custom Linux Shell in C

Language Platform Status

A minimal Linux shell written in C that reads commands from a prompt, executes them using system calls, supports pipes, handles signals, and manages background jobs — just like a real terminal.


🔍 How It Works

User types a command
        ↓
  get_command()        ← extract command name
        ↓
check_command_type()   ← BUILTIN / EXTERNAL / NO_COMMAND
        ↓              ↓
execute_internal()   fork() + execvp()

✨ Features

  • 💬 Custom prompt — change anytime with PS1=newprompt
  • 🔧 Built-in commandscd, pwd, echo, exit, jobs, fg, bg
  • External commands — 150+ Linux commands via fork() + execvp()
  • 🔗 Pipes — chain commands using |
  • 📡 Signal handlingCtrl+C and Ctrl+Z handled gracefully
  • 🌙 Job control — suspend, list, and resume background processes

🗂️ Project Structure

Minishell/
├── main.c          # Entry point — initializes prompt and starts the shell
├── scan_input.c    # Shell loop — reads input, routes commands, handles signals
├── commands.c      # Executes built-in and external commands, handles pipes
├── header.h        # Structs, macros, and all function declarations
└── ext_cmds.txt    # List of supported external commands (loaded at startup)

🚀 Build & Run

Prerequisites

  • GCC compiler
  • Linux / Unix environment

Compile

gcc main.c scan_input.c commands.c -o minishell

Run

./minishell

📖 Usage

Basic commands

minishell$: pwd
/home/amar

minishell$: cd /tmp

minishell$: ls | grep .c
main.c
scan_input.c
commands.c

Built-in commands

minishell$: echo $$        # shell PID
minishell$: echo $?        # last exit status
minishell$: echo $SHELL    # shell path

Job control

minishell$: sleep 100      # press Ctrl+Z to suspend
minishell$: jobs
[1] Stopped  sleep 100
minishell$: fg             # resume in foreground
minishell$: bg             # resume in background

Custom prompt

minishell$: PS1=dev>
dev>

⚙️ Signal Handling

Signal Shortcut What happens
SIGINT Ctrl+C Cancels current command, re-shows prompt
SIGTSTP Ctrl+Z Suspends process, adds it to jobs list
SIGCHLD auto Cleans up finished background processes

🧠 Key Concepts Used

Concept Purpose
fork() + execvp() Run external commands in a child process
pipe() + dup2() Connect stdout of one command to stdin of the next
waitpid() Wait for child; detect stopped jobs
signal() + kill() Handle Ctrl+C, Ctrl+Z and resume jobs
getcwd() + chdir() Implement pwd and cd
File I/O (open, read) Load ext_cmds.txt at startup

👤 Author

Amar C M — Embedded Systems Engineer

LinkedIn · [email protected]


📜 License

MIT License — free to use and modify with attribution.

Releases

No releases published

Packages

 
 
 

Languages