What a shell is, and Bash vs Zsh

Beginner · 6 min

A shell reads your commands and starts programs. See how it differs from the terminal, what $SHELL really tells you, and where Bash and Zsh disagree.

Eric Murphy · 14 min

What a shell is, and Bash vs Zsh

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Eric MurphyReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A shell is an ordinary program that reads a line of text, works out which program you meant, starts it, and waits for it to finish.
  • The terminal is the window that shows text and takes keystrokes. The shell is the program running inside it, and you can swap one without the other.
  • $SHELL names your login shell from the account database. It does not tell you which shell is running right now.
  • Bash and Zsh look alike at the prompt but differ in scripts, for example in word splitting and array numbering, so a script needs a #! line naming its shell.
  • macOS ships Bash 3.2.57 at /bin/bash and uses Zsh as the default login shell. This roadmap runs Bash 5.3.

Why a shell exists

A computer’s kernel can start programs, open files and send data over a network. It cannot read a line you typed and guess what you meant. Something has to sit between your keyboard and the kernel: read text, find the program named in it, ask the kernel to run that program, wait, and then ask you for the next thing.

That something is the shell. It is not part of the operating system in any magical way. It is a normal program, one you could replace tomorrow. Think of a dispatcher at a front desk: you hand over a request, the dispatcher finds the right worker, sends them off, and takes the next request when the worker comes back.

The terminal is not the shell

People say “terminal” and “shell” as if they were one thing. They are two programs.

  • The terminal (Terminal.app, iTerm, a VS Code panel) draws characters in a window and sends your keystrokes onward.
  • The shell is what receives those keystrokes and does the work.

You can see this by asking which process is running in the window. I ran this in a clean environment, with Bash 5.3 from Homebrew first on the PATH:

$ ps -p $$ -o comm=
/opt/homebrew/bin/bash

$$ is the process ID of the current shell. Change the shell and the terminal doesn’t care.

Bash, Zsh and the rest

Shells come in families. The Bourne shell (sh) came first in the 1970s, and a POSIX standard later froze its core. Bash is the GNU project’s extension of that language. Zsh is another extension with a friendlier interactive side. Fish is a third, deliberately incompatible one. I didn’t run those history claims, they are the usual account.

What I did check is what a Mac has. /etc/shells lists the shells the system accepts as login shells:

$ cat /etc/shells | grep -v '^#'
/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

and the versions:

$ /bin/bash --version | head -1
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin26)
$ zsh --version
zsh 5.9 (arm64-apple-darwin26.0)
$ echo $BASH_VERSION
5.3.20(1)-release

The system Bash is 3.2.57, a very old release. The usual explanation, which I did not verify, is that newer Bash versions use the GPLv3 licence and Apple stopped shipping them. Zsh is the default interactive shell on current macOS. The last line is a second Bash, installed with Homebrew. Everything in this roadmap runs on Bash 5.3. If your bash prints a 3.2 version, some features in later lessons, such as associative arrays, will not exist for you. Linux distributions usually ship Bash 5.x.

$SHELL does not mean what you think

The common advice is “run echo $SHELL to see your shell”. That is wrong in a way that bites. $SHELL holds your login shell, the one the account database says to start at login. It is set once and inherited. Start a different shell by hand and it doesn’t change:

$ echo "SHELL=$SHELL  BASH_VERSION=$BASH_VERSION"
SHELL=/bin/zsh  BASH_VERSION=5.3.20(1)-release
$ zsh -c 'echo "zsh sees SHELL=$SHELL, ZSH_VERSION=$ZSH_VERSION"'
zsh sees SHELL=/bin/zsh, ZSH_VERSION=5.9

Both processes see SHELL=/bin/zsh, and one of them is Bash. To know which shell is running, ask the shell itself: $BASH_VERSION is set only in Bash, $ZSH_VERSION only in Zsh.

Where Bash and Zsh disagree

At the prompt, ls, cd and pipes behave the same, which is why people think the shells are interchangeable. Scripts show the seams. I saved this file and ran it with each shell:

v="a b c"
for x in $v; do echo "[$x]"; done
a=(x y z)
echo "first element: [${a[0]}]"
$ bash split.sh
[a]
[b]
[c]
first element: [x]
$ zsh split.sh
[a b c]
first element: []

Bash splits the unquoted $v into three words, which Zsh deliberately doesn’t. Bash numbers array elements from 0, Zsh from 1, so ${a[0]} is empty there. Neither is a bug. They are different languages that share a prompt.

That is why a script names its interpreter on the first line, a #! line, which a later lesson covers in your first script. A file with no #! runs in whatever shell launched it.

About the video

This is Eric Murphy’s “Bash vs ZSH vs Fish: What’s the Difference?”, published 22 November 2021, 13:32 long, in English by its title and description. It is clipped here to the first 5 minutes 36 seconds, which per the description’s chapters is the intro and the Bash and Zsh sections. The Fish section and the recommendation come after, and I left them out of the clip because Fish isn’t taught here. I couldn’t read a transcript, so what I can vouch for is the title, description and chapter list, not what he says in detail. It is five years older than this lesson; the relevant facts about the shells haven’t moved much, but it was made before Bash 5.3 and I can’t say whether it mentions macOS’s move to Zsh.

What I did not test

I did not run chsh, which changes your login shell, since that edits a real account. I ran nothing on Linux, and I did not run Fish.

Where this goes next

You now know the thing at the prompt is a replaceable program with a specific version. The next lesson takes apart what you type to it: anatomy of a command.

Check yourself

  1. 01You open a terminal window and type a command. Which of the two programs, the terminal or the shell, looks up the command and starts it?

    The shell. The terminal only draws characters and passes keystrokes to whatever program is attached, which is usually a shell.

  2. 02You run echo $SHELL inside Bash and it prints /bin/zsh. Are you in Zsh?

    Not necessarily. $SHELL is your login shell from the account database, and a shell started by hand does not update it. Check $BASH_VERSION or ps -p $$ -o comm= instead.

  3. 03Why does a script that works in Bash sometimes misbehave when run with Zsh?

    The languages differ in details. Zsh does not split an unquoted variable into words by default, and its arrays start at 1, so the same text can mean different things.

  4. 04Why would you put #!/usr/bin/env bash at the top of a script?

    It tells the system which interpreter should run the file, so the script behaves the same no matter which shell the person happened to launch it from.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark