TExcelTAD
Home › Terminal & Command Line › Shells Compared

Shells Compared

A shell is two things at once: an interactive tool (you type commands one at a time) and a programming language (the same commands saved in a script, with variables, conditions and loops). The four shells in this course share the interactive basics but differ a lot as languages.

The four shells at a glance

CMD PowerShell bash zsh
Full name Command Prompt, cmd.exe PowerShell 7 (pwsh); Windows PowerShell 5.1 (powershell.exe) Bourne Again SHell Z shell
First released 1987 (OS/2), Windows NT 1993 2006 (open source and cross-platform since 2016) 1989 1990
Runs on Windows only Windows, macOS, Linux Linux, macOS, Windows (WSL, Git Bash) macOS, Linux, Windows (WSL)
Default on — (still installed everywhere) Windows 11 terminal most Linux distros macOS since 10.15
Script files .bat, .cmd .ps1 .sh .zsh / .sh
Passes between commands text .NET objects text text
Variables %name% $name $name $name
Case-sensitive commands? no no yes yes
Comment rem or :: # # #

bash and zsh are both POSIX-style shells: they descend from the Unix Bourne shell (sh) and accept most of the same syntax. zsh adds many interactive comforts and a few language differences. CMD and PowerShell are separate Windows traditions with nothing in common syntactically, even though PowerShell defines aliases like dir, cd and copy so CMD habits keep working.

The same script in four shells

Let's write a tiny script in each shell: store a name, loop over three fruits, and print a message if a number is big enough. Compare how each language spells the same ideas.

Windows (CMD)

cmd · try it yourself
@echo off
set name=Ada
echo Hello, %name%
for %%f in (apple banana cherry) do echo fruit: %%f
set /a total=7*6
if %total% gtr 40 echo %total% is more than 40
Hello, Ada
fruit: apple
fruit: banana
fruit: cherry
42 is more than 40

CMD's language is minimal: set makes variables, set /a does integer arithmetic, and comparisons use words such as gtr (greater than) and equ. Loop variables in a batch file need double percent signs (%%f).

Windows (PowerShell)

powershell · try it yourself
$name = 'Ada'
"Hello, $name"
foreach ($f in 'apple', 'banana', 'cherry') { "fruit: $f" }
$total = 7 * 6
if ($total -gt 40) { "$total is more than 40" }
Hello, Ada
fruit: apple
fruit: banana
fruit: cherry
42 is more than 40

PowerShell looks like a modern programming language: real arithmetic, curly-brace blocks, and comparison operators that start with a dash (-gt, -eq, -like). A string on its own line is simply output.

Linux (bash)

bash · try it yourself
name="Ada"
echo "Hello, $name"
for f in apple banana cherry; do echo "fruit: $f"; done
total=$((7 * 6))
if [ "$total" -gt 40 ]; then echo "$total is more than 40"; fi
Hello, Ada
fruit: apple
fruit: banana
fruit: cherry
42 is more than 40

In bash, name="Ada" must have no spaces around =. Arithmetic goes inside $(( )), and [ ... ] is the test command.

macOS (zsh)

The bash version runs unchanged in zsh. Here is a slightly more "zsh" way to write it, with an array (zsh arrays start at index 1, bash arrays at 0):

zsh · try it yourself
name="Ada"
print "Hello, $name"
fruits=(apple banana cherry)
for f in $fruits; do print "fruit: $f"; done
print "first fruit: $fruits[1]"
(( total = 7 * 6 ))
(( total > 40 )) && print "$total is more than 40"
Hello, Ada
fruit: apple
fruit: banana
fruit: cherry
first fruit: apple
42 is more than 40

Text versus objects

The biggest conceptual split is what flows through a pipe |. In CMD, bash and zsh it's text: the next command receives lines of characters and has to cut them apart. In PowerShell it's objects with named properties, so you can sort or filter by a property without parsing text:

powershell · try it yourself
'banana', 'fig', 'cherry' | Sort-Object Length | ForEach-Object { "$_ has $($_.Length) letters" }
fig has 3 letters
banana has 6 letters
cherry has 6 letters

The bash version works on text, so it first has to produce the number and then sort by it:

bash · try it yourself
for w in banana fig cherry; do echo "${#w} $w"; done | sort -n -s -k1,1 | while read -r n w; do echo "$w has $n letters"; done
fig has 3 letters
banana has 6 letters
cherry has 6 letters

Both styles are powerful; PowerShell Pipeline and Pipes & Redirection explore them.

Which shell should I learn?

Your situation Learn first Then
Windows user or admin PowerShell enough CMD to read old .bat files
Web or backend developer bash (it's on every server and container) zsh on a Mac is nearly identical
macOS user zsh (interactive), bash-compatible scripting
You maintain old Windows build scripts CMD & batch PowerShell for new scripts

Other shells exist too: fish (very friendly, not POSIX-compatible), sh/dash (minimal POSIX shell used for system scripts on Debian and Ubuntu), ksh, and Nushell (structured data, like PowerShell). Skills from the four shells here transfer to all of them.

To try another shell, just type its name. It starts inside your current shell, and exit brings you back:

text
ada@ubuntu:~$ zsh
ubuntu% echo $ZSH_NAME
zsh
ubuntu% exit
ada@ubuntu:~$

Learn more: Linux & Bash Intro · PowerShell Intro · CMD Intro · zsh Essentials

Test yourself

Which shell passes .NET objects, not text, through the pipeline?

  1. CMD
  2. bash
  3. zsh
  4. PowerShell

In zsh, fruits=(apple banana cherry). What is $fruits[1]?

  1. banana
  2. apple
  3. an error
  4. an empty string

Which line correctly assigns a variable in bash?

  1. name = Ada
  2. set name=Ada
  3. name=Ada
  4. $name = 'Ada'