Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

bash / sh / zsh Command Guide

Which shell are you actually using, and does it matter?

What Are bash, sh, and zsh?

bash, sh, and zsh are all shells — the programs that read your commands and hand them to the kernel (see Terminal vs Shell vs Kernel). sh is the original, minimal POSIX shell. bash (Bourne Again SHell) extends it with arrays, better scripting, and is the default on most Linux systems. zsh adds extras on top of bash-like syntax — spell correction, better completion, themes — and is the default on macOS.

Why Learn the Difference?

  • Your scripts’ shebang depends on it: #!/bin/bash and #!/bin/sh behave differently.
  • Your startup file depends on it: ~/.bashrc for bash, ~/.zshrc for zsh, ~/.profile for login shells.
  • “It worked on my machine” bugs are frequently shell bugs — one shell’s feature is another’s error.

Learn which shell you are in (echo $SHELL, echo $0, echo $BASH_VERSION) and the three names stop being confusing. They are one family tree, and you now know the branches.

1. Checking Your Shell

echo $SHELL          # your default (login) shell
echo $0              # the shell running this session
echo $BASH_VERSION   # non-empty only in bash
echo $ZSH_VERSION    # non-empty only in zsh
ps -p $$             # the process name of the current shell

2. The Three, Compared

Featureshbashzsh
Full nameBourne shell (POSIX)Bourne Again SHellZ shell
Default whereMinimal systems, /bin/sh linkMost Linux distrosmacOS (since Catalina)
Startup file~/.profile~/.bashrc, ~/.bash_profile~/.zshrc
ArraysNoYes (indexed + associative)Yes
Scripting extrasNone[[ ]], local, arithmeticRich prompt, modules
Themes / pluginsNoLimited (via prompt)Extensive (Oh My Zsh)
POSIX complianceThe referenceMostlyMostly

3. Startup Files by Shell

ShellInteractive, non-loginLogin shell
bash~/.bashrc~/.bash_profile (or ~/.profile)
zsh~/.zshrc~/.zprofile
sh~/.profile~/.profile
source ~/.bashrc     # reload bash config after editing
source ~/.zshrc      # reload zsh config after editing

4. Writing Scripts That Work Everywhere

#!/bin/sh            # POSIX-only features: runs in sh, bash, zsh
#!/bin/bash          # bash-only features: arrays, [[ ]], local
# Portable test — works in every shell:
if [ "$a" = "$b" ]; then ... fi

# bash/zsh extension — do not use in #!/bin/sh scripts:
if [[ "$a" == "$b" ]]; then ... fi

Common Pitfalls

MistakeFix
Edited ~/.bashrc but use zshEdit ~/.zshrc instead; check echo $SHELL first
#!/bin/bash script fails on a minimal systemSome systems only ship sh; use #!/bin/sh + POSIX syntax
Bash-only syntax ([[ ]], arrays) in an sh scriptKeep scripts POSIX, or switch the shebang to bash
“Command not found” in a script that works interactivelyScripts don’t load ~/.bashrc; check PATH inside the script
macOS sh behaving oddlymacOS /bin/sh is bash in POSIX mode — fine, but test on your target
Confusing $SHELL with the running shell$SHELL is your default; $0 or ps -p $$ is the truth now
PageWhat It Covers
Terminal vs Shell vs KernelWhere shells sit in the big picture
Environment VariablesStartup files and export in detail
AliasesPer-shell alias configuration
HistoryPer-shell history files
Shell ScriptingThe syntax all three shells share
Opening a TerminalWhich shell your terminal actually starts

Shells Cheatsheet

TaskCommand
Your default shellecho $SHELL
Currently running shellecho $0 or ps -p $$
Am I in bash?echo $BASH_VERSION
Am I in zsh?echo $ZSH_VERSION
Reload bash configsource ~/.bashrc
Reload zsh configsource ~/.zshrc
Portable script shebang#!/bin/sh
Bash script shebang#!/bin/bash
Change your default shellchsh -s /bin/zsh