A shell is a program that lets you control a Unix system by typing commands. Common shells include Bash, Zsh, Fish, and Dash.
When you open a terminal, you usually see a terminal emulator running a shell:
terminal emulator → PTY → shell → programs
The terminal emulator draws the screen and sends your keystrokes. The PTY gives the shell a terminal-shaped connection. The shell reads your command, starts the requested program, and prints the result.
A shell reads commands
When you type:
ls -la
the shell:
- reads the line;
- splits it into a command and arguments;
- finds the
lsprogram; - starts it as a child process;
- waits for it to finish;
- prints another prompt.
The shell does not usually implement ls itself. It starts the ls program and connects that program to the terminal.
A shell connects programs
The shell also provides syntax for composing programs:
cat access.log | grep 500 > errors.txt
Here the shell:
- starts
catandgrep; - connects
cat's output togrep's input with a pipe; - writes
grep's output toerrors.txt; - manages the whole pipeline as one job.
This is why small Unix programs can do useful work together. The shell acts as the glue between them.
Shell features
A shell commonly provides:
- commands, such as
cd,export, andalias; - program launching, such as
git status; - pipes, using
|; - redirection, using
<,>, and>>; - variables, such as
$HOME; - globbing, such as
*.log; - conditionals and loops;
- scripts, which are saved sequences of shell commands;
- job control, including
&,fg,bg, andCtrl-Z.
Some commands are builtins. cd is the classic example. A child process cannot change the shell's current directory, so the shell must implement cd itself.
Other commands are separate executable files. You can often find one with:
command -v git
Shell versus terminal
These are different programs:
- the terminal emulator displays text and sends input;
- the PTY provides the terminal-shaped connection;
- the shell reads commands and starts programs;
- the program does the work requested by the command.
For example, when you run vim in a terminal, vim is not part of the shell. The shell starts it, and the PTY connects it to the terminal emulator.
A shell is therefore best understood as a command interpreter and process launcher. It is the layer that turns typed text into running programs and connected streams.