🧠 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:

TypeDescriptionExample
Command-Line Interface (CLI)Text-based interactionbash, zsh, sh, cmd.exe, PowerShell
Graphical User Interface (GUI)Visual windows/buttonsGNOME, KDE, Windows Explorer

When we say β€œshell” in system interviews, we’re usually talking about CLI shells like Bash.


πŸ› οΈ What Does a Shell Do?

FunctionDescription
🧾 Takes user inputReads commands from the keyboard or a script
πŸ”„ Parses commandsBreaks input into program name, args, options
πŸ” Executes commandsCalls system calls (e.g., fork(), exec()) to run programs
🧠 Handles I/O redirectionSupports >, <, >>, `
πŸ” Handles variables & scriptingSupports loops, conditionals, variables
πŸ’‘ Provides environmentSets 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

ShellFeatures
sh (Bourne Shell)Original UNIX shell
bash (Bourne Again SHell)Most common, scripting support
zshInteractive shell, themes (Oh My Zsh)
fishUser-friendly, autocomplete
csh/tcshC-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 file

These 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

ComponentRole
KernelCore OS β€” manages hardware & resources
ShellInterface to talk to kernel
TerminalTool to display shell input/output