What Is a Shell?
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:
- You type a human-readable command.
- The shell interprets it.
- The shell asks the kernel, through system calls, to start the program you named.
- 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
shbelong 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
mvis.
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:
- Display the prompt.
- Read a line.
- Parse and expand the line.
- Run the command.
- Collect the exit status.
- 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:
| Shell | Program name | Notes |
|---|---|---|
| Bourne shell | sh | The original Unix shell from the 1970s; the model for the POSIX standard |
| Bash | bash | The Bourne Again Shell, the default on most Linux systems |
| Dash | dash | A small, fast POSIX shell, often used as /bin/sh on Debian and Ubuntu |
| Zsh | zsh | An extended shell with rich completion; the default on recent macOS |
| Fish | fish | A 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.