Zsh and bash are both Unix shells that run the same everyday commands, but they differ in scripting rules and in interactive features. The differences that most often break things are array numbering (zsh starts at 1, bash at 0), word splitting of unquoted variables, and which startup file each one reads.
Since macOS 10.15 (Catalina), Apple's default login and interactive shell is zsh, so many people meet zsh by accident and then hit a bash script that behaves oddly. The sections below list the differences that change results, each with the manual that documents it.
Which shell is the default?
Apple's support page says that starting with macOS 10.15, a Mac uses zsh as the default login shell and interactive shell. Bash was the default on macOS 10.14 and earlier. Most Linux distributions are different: bash is the usual default there, though this varies by distribution and you should check yours.
To see what you are running right now:
echo $0
echo $SHELL
The first command prints the current shell, the second your login shell. Example output on a Mac with the default setup:
-zsh
/bin/zsh
To change the login shell, use chsh -s with a path listed in /etc/shells:
chsh -s /bin/bash
Quick comparison
| Area | Bash | Zsh |
|---|---|---|
| First array element | Index 0 | Index 1 (index 0 if KSH_ARRAYS is set) |
Unquoted $var with spaces | Split into words | Not split (unless SH_WORD_SPLIT is set) |
| Glob that matches nothing | See the bash manual (nullglob, failglob) | Error by default (NOMATCH is on) |
| Interactive startup file | ~/.bashrc (non-login shells) | ~/.zshrc (all interactive shells) |
| Login startup file | First of ~/.bash_profile, ~/.bash_login, ~/.profile | ~/.zprofile, then ~/.zlogin |
| Shared history between terminals | Not described in the options manual here | Off by default (SHARE_HISTORY) |
Array indexing: zsh starts at 1, bash at 0
The GNU Bash manual says plainly: "Indexing starts at zero." The zsh manual says array elements are numbered beginning with 1, unless the KSH_ARRAYS option is set, in which case they are numbered from zero.
fruits=(apple banana cherry)
echo "${fruits[0]}"
echo "${fruits[1]}"
Example output in bash:
apple
banana
Example output in zsh:
apple
In zsh the first line is empty. Per the manual, reading subscript 0 returns an empty string when KSH_ARRAYS is off, and writing to it is an error. A script that loops from index 0 therefore skips or misreads an element in zsh. If you need one script to work in both, run it with bash by adding a #!/bin/bash line, rather than trying to write array code that satisfies both.
The same numbering rule applies to the positional parameters array: with KSH_ARRAYS set, zsh makes ${argv[0]} equal to $1.
Word splitting of unquoted variables
The zsh manual calls this "an important difference from other shells": the results of unquoted parameter expansions are not split on whitespace unless SH_WORD_SPLIT is set. That option is off in zsh by default and on only when zsh emulates ksh or sh.
files="a.txt b.txt"
for f in $files; do echo "item: $f"; done
Example output in bash (the string is split into two words):
item: a.txt
item: b.txt
Example output in zsh (one word, because there is no splitting):
item: a.txt b.txt
In both shells the safest habit is to use an array for a list of items and quote the expansion, instead of relying on splitting a string. If you want bash-like splitting inside zsh, run setopt SH_WORD_SPLIT, though that changes behaviour for everything that follows in that session.
Globs that match nothing
By default zsh reports an error when a filename pattern matches no files. The manual says the word is deleted instead only if NULL_GLOB is set, and left unchanged if NOMATCH is unset.
ls *.xyz
Example output in zsh when no .xyz file exists:
zsh: no matches found: *.xyz
This commonly shows up with commands such as pip install package[extra], where the square brackets are read as a glob pattern. Quote the argument ('package[extra]') and zsh passes it through untouched.
Startup files: where your settings go
People often move a ~/.bashrc to a Mac and wonder why nothing loads. Zsh reads different files.
- Zsh:
/etc/zshenvand$ZDOTDIR/.zshenvfor every shell. Login shells then read.zprofileand.zlogin. Interactive shells read.zshrc. IfZDOTDIRis unset, zsh usesHOME. - Bash: an interactive login shell reads
/etc/profile, then the first readable of~/.bash_profile,~/.bash_loginand~/.profile. An interactive non-login shell reads only~/.bashrc.
Put aliases and prompt settings for zsh in ~/.zshrc. Terminal apps often start login shells, which is why bash users commonly make ~/.bash_profile source ~/.bashrc. That step is not needed for zsh, because .zshrc is read by interactive shells whether or not they are login shells.
Interactive features: options you turn on
Several zsh conveniences exist as options that are off by default, so they only help once you enable them in ~/.zshrc. All four below are documented in the zsh Options manual:
AUTO_CD: if a command can't be run as a normal command and is the name of a directory, zsh changes into that directory.CORRECT: tries to correct the spelling of commands.EXTENDED_GLOB: treats#,~and^as part of filename patterns.SHARE_HISTORY: imports new commands from the history file and shares history between running shells.
setopt AUTO_CD
setopt SHARE_HISTORY
Putting those lines in ~/.zshrc makes them permanent. Because EXTENDED_GLOB gives #, ~ and ^ special meaning, turning it on can change how existing commands with those characters are read.
Should you switch, and what about scripts?
For typing commands, either shell works, and zsh is already what a new Mac gives you. For scripts you share or run on servers, write them for bash and say so on the first line. The zsh FAQ itself says bash and zsh are different programming languages that are not interchangeable, and recommends keeping bash scripts as bash scripts instead of porting them. For a primer on writing one, see Bash script basics.
If a script fails only on a Mac, check these in order: array index starting at 0, an unquoted variable relied on to split, a glob or bracket argument that zsh rejects, and a setting that lives in ~/.bashrc instead of ~/.zshrc.
Frequently asked questions
- Is zsh better than bash?
- Neither is better in general. Zsh has more interactive features that you can switch on, such as spelling correction, automatic directory changes and extended globbing. Bash is the common target for shell scripts on Linux. Which one suits you depends on whether you mostly type commands or mostly write portable scripts.
- Can I run a bash script in zsh?
- Often yes, but not always. The zsh FAQ says bash and zsh are different programming languages and not interchangeable. Differences such as array numbering and word splitting can change what a script does. Put #!/bin/bash on the first line so the script runs under bash whichever shell you type in.
- How do I check which shell I am using?
- Run echo $0 to see the name of the current shell. Run echo $SHELL to see your login shell, which can differ if you started another shell inside it. Example output on a Mac with default settings: -zsh for the first, /bin/zsh for the second.
- How do I switch the default shell on a Mac?
- Run chsh -s /bin/bash or chsh -s /bin/zsh in Terminal. The path must be listed in /etc/shells. You can also change it in System Settings under Users & Groups, Advanced Options. Open a new Terminal window for the change to apply.
Sources
- Apple Support: Use zsh as the default shell on your Mac
- Zsh Manual: Parameter Expansion (word splitting)
- Zsh Manual: Parameters (array subscripts, KSH_ARRAYS)
- Zsh Manual: Options
- Zsh Manual: Startup/Shutdown Files
- Zsh FAQ: Differences from other shells
- GNU Bash Reference Manual: Arrays
- GNU Bash Reference Manual: Bash Startup Files