π§ What is a Shell?
A Shell is a user interface to the Operating System β it allows users to interact with the OS kernel, typically by typing commands.
Think of the shell as the middleman between you and the kernel.
It takes your commands β translates them β and passes them to the kernel to execute.
π» Types of Shells
There are two main types of shells:
| Type | Description | Example |
|---|---|---|
| Command-Line Interface (CLI) | Text-based interaction | bash, zsh, sh, cmd.exe, PowerShell |
| Graphical User Interface (GUI) | Visual windows/buttons | GNOME, KDE, Windows Explorer |
When we say βshellβ in system interviews, weβre usually talking about CLI shells like Bash.
π οΈ What Does a Shell Do?
| Function | Description |
|---|---|
| π§Ύ Takes user input | Reads commands from the keyboard or a script |
| π Parses commands | Breaks input into program name, args, options |
| π Executes commands | Calls system calls (e.g., fork(), exec()) to run programs |
| π§ Handles I/O redirection | Supports >, <, >>, ` |
| π Handles variables & scripting | Supports loops, conditionals, variables |
| π‘ Provides environment | Sets PATH, HOME, PS1, etc. |
π Shell Workflow (CLI)
You type: ls -la /home/gaurav
β
Shell parses: command = ls, args = -la /home/gaurav
β
Shell uses fork() to create a new process
β
Child process uses exec() to run `/bin/ls`
β
Kernel executes ls, returns output
β
Shell prints output to your terminal
π§ Common Shells in Linux
| Shell | Features |
|---|---|
| sh (Bourne Shell) | Original UNIX shell |
| bash (Bourne Again SHell) | Most common, scripting support |
| zsh | Interactive shell, themes (Oh My Zsh) |
| fish | User-friendly, autocomplete |
| csh/tcsh | C-like syntax |
You can check your shell with:
echo $SHELLπ§ͺ Example Shell Commands
ls -l # List files in long format
cd /var/log # Change directory
cat file.txt # Print file contents
echo "Hello" > a.txt # Output to a fileThese are interpreted by the shell, which then uses system calls under the hood to execute them.
π Shell Script Example
#!/bin/bash
echo "Welcome, $USER"
date- This script runs a sequence of commands via the shell.
π§ Interview-Ready Definition:
A Shell is a program that provides a user interface to the operating system, typically via a command-line interface. It interprets user commands, interacts with the kernel using system calls, and supports features like scripting, I/O redirection, and environment management.
β Shell vs Kernel vs Terminal
| Component | Role |
|---|---|
| Kernel | Core OS β manages hardware & resources |
| Shell | Interface to talk to kernel |
| Terminal | Tool to display shell input/output |