What Is a Shell?

Su Suriya Ravichandran Updated 06 Dec 2026
6 min read ·Lesson 2 of 205

A shell is a program that lets you control an operating system by giving it text instructions. It sits between you and the kernel, accepts commands, starts programs on your behalf, and reports what happened. The name comes from the idea that the shell is an outer layer wrapped around the kernel, much like a shell around a nut.

Understanding the Concept

The kernel is the part of the operating system that has full access to the hardware. It decides which program runs on the processor, how memory is shared, and how files are read from and written to storage. Ordinary programs, including the shell itself, run in a restricted mode and must ask the kernel to do privileged work through system calls.

A person cannot type a system call directly. A shell fills this gap:

  1. You type a human-readable command.
  2. The shell interprets it.
  3. The shell asks the kernel, through system calls, to start the program you named.
  4. The program runs and its output is shown to you.
You  →  Shell  →  Kernel  →  Hardware

The shell is therefore a user interface to the operating system. It happens to be a text interface instead of a graphical one, which is why it is often called a command-line interface (CLI).

Command-Line Shells and Graphical Shells

The word "shell" is used in two senses:

  • A command-line shell takes typed commands. Bash, Zsh, Fish, and sh belong to this group.
  • A graphical shell is the desktop environment that shows windows, icons, and menus. Dragging a file to a folder is a request to the operating system, just like mv is.

In this tutorial, "shell" always means a command-line shell.

What a Shell Does

A command-line shell has several responsibilities that happen every time you press Enter.

Reading and Parsing Input

The shell reads a line and splits it into words. The first word is normally the command, and the remaining words are its arguments:

ls -l /etc

Here ls is the command, while -l and /etc are arguments. The shell passes the arguments to the program without understanding what they mean. Only ls knows that -l requests a long listing.

Expanding Special Characters

Before running a command, the shell rewrites certain patterns. For example, the asterisk matches file names:

touch a.txt b.txt c.log
ls *.txt

Output:

a.txt
b.txt

The shell replaces *.txt with the matching file names a.txt b.txt before ls starts. The ls program never sees the asterisk. This behavior, called expansion, is one of the most important ideas in shell programming.

Starting Programs and Managing Them

When the first word is the name of a program on disk, the shell creates a new process, runs the program inside it, and waits until it finishes. The shell can also run programs in the background, connect their input and output, and stop or resume them.

Providing Built-In Commands

Some commands are implemented inside the shell itself instead of being separate programs. The type command reveals which is which:

type cd
type ls

Output on a typical Linux system:

cd is a shell builtin
ls is /usr/bin/ls

The cd command must be built in. A separate program cannot change the current directory of the shell that started it, because a child process can only change its own directory. A later lesson covers built-in and external commands in depth.

Offering a Programming Language

A shell is also an interpreter for a scripting language. It supports variables, conditions, loops, functions, and arrays, so you can write programs that run unattended.

The Prompt and the Command Loop

A shell displays a prompt whenever it is ready for a new command. The usual cycle is:

  1. Display the prompt.
  2. Read a line.
  3. Parse and expand the line.
  4. Run the command.
  5. Collect the exit status.
  6. Return to step 1.

Every command finishes with an exit status, a number the shell stores in the special variable ?. By convention, zero means success and any other value means failure:

true
echo $?
false
echo $?

Output:

0
1

The true command always succeeds and false always fails. Checking $? immediately after a command shows how it ended. Scripts rely on these numbers to decide what to do next.

Input, Output, and Pipes

Every program started by a shell receives three standard channels: standard input, standard output, and standard error. The shell can rewire them. Sending output into a file and into another program are two examples:

echo "Chennai" > cities.txt
echo "Madurai" >> cities.txt
cat cities.txt | sort

Output:

Chennai
Madurai

The first line creates the file with >, which replaces any existing content. The second line adds to it with >>. The third line sends the file's content through a pipe into sort, which prints the lines in alphabetical order. Because both lines already happen to be in order, the output looks unchanged.

Interactive and Non-Interactive Shells

A shell can run in two modes:

  • Interactive: it prints a prompt and waits for you to type. This is what you see in a terminal.
  • Non-interactive: it reads commands from a file or from another program and runs them without a prompt. Scripts run this way.

Both modes use the same language, which is why you can test an idea at the prompt and then paste it into a script. A later lesson explains the differences, such as which start-up files each mode reads.

Finding Out Which Shells Exist

The file /etc/shells lists the shells that the system recognizes as valid login shells:

cat /etc/shells

Sample output:

# /etc/shells: valid login shells
/bin/sh
/usr/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/usr/bin/dash

The exact list depends on what is installed. Each line is a path to a shell program. The variable SHELL contains the path of your default login shell:

echo $SHELL

Output:

/bin/bash

Note that SHELL records your configured login shell, not necessarily the shell running at this moment. If you start a different shell from inside your terminal, SHELL does not change.

Common Shells

Many shells exist, each with its own history and strengths:

ShellProgram nameNotes
Bourne shellshThe original Unix shell from the 1970s; the model for the POSIX standard
BashbashThe Bourne Again Shell, the default on most Linux systems
DashdashA small, fast POSIX shell, often used as /bin/sh on Debian and Ubuntu
ZshzshAn extended shell with rich completion; the default on recent macOS
FishfishA user-friendly shell with suggestions as you type, not POSIX compatible

They share many ideas, but their syntax differs in details. A script written for one shell may fail in another, which is why scripts declare their interpreter with a shebang line.

A Shell Is Just a Program

The shell is an ordinary program stored on disk, and you can inspect it like any other file:

ls -l /bin/bash

Sample output:

-rwxr-xr-x 1 root root 1446024 Mar 31  2024 /bin/bash

The leading -rwxr-xr-x shows that it is a regular file that everyone can execute. Because it is just a program, you can start another shell inside your current one, replace it, or run it with a script as its argument. The next lessons build on that idea by looking at Bash in particular and by separating the terms shell, terminal, and Bash.

0 Comments

Reviewed before they appear

No comments yet.

Bash
Introduction to Bash What Is a Shell? What Is Bash (Bourne Again Shell)? Bash vs. Shell vs. Terminal Bash vs. sh, Zsh, and Fish Where Bash Is Used: Linux, DevOps, and Automation Installing Bash on Linux Using Bash on macOS Using Bash on Windows with WSL Checking Your Bash Version Opening and Using a Terminal Running Your First Bash Command Understanding Command Syntax and Arguments Bash Built-in Commands vs. External Commands Working with pwd, ls, and cd Creating Files and Directories with touch and mkdir Copying, Moving, and Renaming Files Removing Files and Directories Safely Viewing Files with cat, less, head, and tail Searching for Files with find Searching Text with grep Understanding File Paths and Wildcards Using man Pages and Command Help Bash Script Structure Creating Your First Bash Script Understanding the Shebang (#!/usr/bin/env bash) Making a Script Executable with chmod Running a Bash Script in Different Ways Writing Comments in Bash Using echo and printf Reading User Input with read Displaying Helpful Messages in Scripts Using Semicolons and Newlines Bash Variables: Declaration and Usage Rules for Naming Bash Variables Environment Variables Local Variables vs. Environment Variables Exporting Variables with export Common Environment Variables: HOME, PATH, USER, and PWD Read-Only Variables with readonly Removing Variables with unset Command-Line Arguments: $0, $1, and More Special Parameters: $#, $?, $$, $!, $@, and $* Positional Parameters and shift Default Values with Parameter Expansion Single Quotes vs. Double Quotes Escaping Special Characters Command Substitution with $(...) Arithmetic Expansion with $((...)) Brace Expansion Tilde Expansion and Home Directories Filename Expansion and Globbing Understanding Word Splitting Parameter Expansion Basics Why Quoting Variables Matters Standard Input, Standard Output, and Standard Error Redirecting Output with > and >> Redirecting Input with < Redirecting Errors with 2> Combining Standard Output and Standard Error Using Pipes (|) to Connect Commands Using tee to Save and Display Output Here Documents with << Here Strings with <<< Command Lists with ;, &&, and || Conditional Statements with if, then, and fi Using elif and else Testing Conditions with test and [ ] Using [[ ]] for Bash Conditionals Comparing Strings in Bash Checking Whether Files Exist Checking File Types and Permissions Integer Comparisons in Bash Logical Operators: &&, ||, and ! Using case Statements Pattern Matching in case Common Conditional Statement Mistakes for Loops in Bash C-Style for Loops while Loops until Loops Looping Through Files and Directories Looping Through Command Output Safely break and continue Nested Loops select Loops for Simple Menus Avoiding Infinite Loops Defining and Calling Bash Functions Passing Arguments to Functions Returning Status Codes from Functions Understanding return vs. echo in Functions Local Variables in Functions Function Scope and Variable Visibility Organizing Scripts into Functions Sourcing Other Bash Files with source Reusable Bash Utility Functions Indexed Arrays in Bash Creating and Accessing Array Elements Adding and Removing Array Elements Looping Through Arrays Array Length and Indices Associative Arrays in Bash Iterating Over Associative Arrays Passing Arrays to Functions Common Array Pitfalls Processing Text with grep, sed, and awk Using cut, sort, uniq, and wc Finding and Replacing Text with sed Extracting Columns with awk Parsing CSV-Like Text from the Command Line Working with Filenames That Contain Spaces Safe File Handling with find -print0 and xargs -0 Temporary Files with mktemp Reading Files Line by Line Appending Logs to a File Understanding Exit Codes Checking Command Success with $? Using exit to End a Script Fail-Fast Scripts with set -e: Benefits and Caveats Using set -u and set -o pipefail Combining Strict Mode Options Safely Handling Errors with if Statements Traps with trap Cleaning Up Temporary Files on Exit Writing Useful Error Messages Debugging Scripts with bash -x Parsing Script Arguments Validating Required Arguments Handling Optional Arguments Parsing Options with getopts Building Help Messages with --help Setting Defaults for Optional Parameters Handling Unknown Options and Invalid Input Creating Interactive Menus in Bash Foreground and Background Processes Running Commands in the Background with & Managing Jobs with jobs, fg, and bg Waiting for Processes with wait Capturing Process IDs with $! and $$ Sending Signals with kill Handling Interrupts and Termination Signals Running Commands with timeout Basic Process Monitoring from Bash Aliases and Shell Functions: When to Use Each Customizing the Bash Prompt (PS1) Bash Startup Files: .bashrc, .bash_profile, and Profile Interactive vs. Non-Interactive Bash Shell Options with set shopt: Enabling Bash-Specific Options Directory Stack with pushd, popd, and dirs Process Substitution with <(...) and >(...) Advanced Parameter Expansion Pattern Matching and Extended Globbing Bash Script Security Fundamentals Avoiding Command Injection in Shell Scripts Quoting User Input Safely Why eval Is Risky and Safer Alternatives Handling Secrets and Credentials in Scripts Using Least Privilege in Automation Scripts Safe Use of rm and Destructive Commands Validating Paths and User-Supplied Filenames ShellCheck: Static Analysis for Bash Scripts Writing Readable and Maintainable Bash Automating Backups with Bash Creating Log Rotation and Cleanup Scripts Checking Disk Usage and Free Space Monitoring CPU, Memory, and System Load Checking Website Availability with curl Automating Updates on Ubuntu and Debian Scheduling Bash Scripts with cron Using Lock Files to Prevent Duplicate Runs Building a Simple Health-Check Script Sending Notifications from Bash Scripts Using Bash in Git Workflows Bash Scripts for Docker Automation Automating Docker Container Health Checks Using Bash in CI/CD Pipelines Environment Configuration for Deployment Scripts Automating SSH Tasks with Bash Deploying a Web Application with a Bash Script Using Bash to Process Application Logs Building Repeatable Development Setup Scripts Bash Debugging Techniques Using set -x and PS4 for Debug Traces Testing Bash Scripts with Bats Mocking Commands During Script Tests Testing Error Paths and Exit Codes Writing Portable Shell Scripts Bash-Specific Syntax vs. POSIX Shell Syntax Using ShellCheck in a Development Workflow Performance Tips for Bash Scripts Build an Interactive Calculator Build a File and Directory Organizer Build a Timestamped Backup Script Build a Log File Analyzer Build a Bulk File Renaming Tool Build a System Resource Monitoring Script Build a Website Uptime Checker Build a User Account Audit Report Build a Database Backup Automation Script Build a Docker Cleanup Helper Build a Server Health-Check and Alert Script Build a Deployment Automation Script Build a Menu-Driven Linux Administration Toolkit Build a Safe Automated Backup and Restore Workflow
Ask about this post
AI Ask about this post

Ask questions about What Is a Shell? and get answers drawn from it.

Signed-in readers only.