Zsh vs Bash
Tags: bash, zsh
Published: 2020-06-09 15:03:07

> This article was originally posted on [StackAbuse.com](https://stackabuse.com/). Check it out for more articles, guides, and more. 👍

When we talk about Unix-based programming, it's usually about the shells, terminals, and the command line interfaces. The most prevalent shell in this regard is [Bash](https://en.wikipedia.org/wiki/Bash_(Unix_shell)) but there are other variants available and used widely as well, like Zsh, or the [Z shell](https://en.wikipedia.org/wiki/Z_shell).

In this article, we'll attempt to draw the line between the two shells and show the differences so you can get a sense of why you might use one or the other. But first, in the following sections we'll introduce both shells before we compare them together. 

# Z Shell

Zsh, or Z shell, was first released by Paul Falstad back in 1990 when he was still a student at Princeton University. Z shell is included in many operating systems, including Mac OS (although it isn't the default that's actually used).

Much like Bash, Z shell can basically be seen as an extended version of the [Bourne shell](https://en.wikipedia.org/wiki/Bourne_shell), and does contain a lot of the same features as Bash, which you'll probably notice in the sections below. You may also notice that it pretty closely resembles the Korn shell as well. Some of the features that are worth mentioning include (but are not limited to):

- File [globbing](https://en.wikipedia.org/wiki/Glob_(programming))
- Spelling correction
- Directory aliases (much like `~` or `..`)
- Loadable modules, like socket controls or an FTP client
- Compatibility modes: e.g. You can use `/bin/bash` as a drop-in replacement for Bash
- Startup/shutdown scripts via `zshenv`, `zprofile`, `zshrc`, `zlogin`, and `zlogout`
- `git` command completion
- Path expansion: e.g. Enter `cd /u/lo/b`, press tab, and it will be completed to `cd /usr/local/bin` since it is the only matching pattern

There are _a lot_ more features than what we've shown here, but at least this gives you an idea as to how shells can be different.

# Bash

The Bash shell (also known as the "Bourne-again shell") was also released around the same period as the Z shell (in 1989) and Brian Fox is regarded as the creator behind it. It was initially written as a replacement for the Bourne shell. For many years it has shipped as the default shell for GNU, most Linux distributions, and Mac OS X (version 10.3+). Like a true replacement should, Bash is capable of executing all of the Bourne shell commands without a problem.

There are quite a few features that the Bash shell has and some of the lesser-known ones include:

- Insert the last parameter(s) of the preceding command in your current command using `Alt` + `.`
- You can keep a process running even after logging out. To do so, use the command `disown -h <pid>` where you will have to place the process ID (PID) of the program instead of `<pid>`
- Execute the previous command again, but this time running with `sudo` using the command `sudo !!` (`!!` is shorthand for 'the previous command')
- Perform a reverse incremental search using the `Ctrl` + `R` keys
- Press tab twice and you will see the list of completions for the word that you have just typed or are typing
- When executing a script with `bash`, use the `-x` option to output the script contents as it's being executed

If you want to learn more, you can see a much larger list of Bash-specific features [here](http://web.mit.edu/gnu/doc/html/features_4.html#SEC20).

# Comparing Z shell and Bash

Now that we've give you a brief introduction to both of the shells, let's see how they hold up when actually compared and contrasted together.

The first thing to look at (and one of the more significant aspects, in my opinion) is prevalence and popularity of the shell. While the Z shell has its fair share of users throughout the developer community, it's usually safer to write your scripts for Bash since there is a much larger group of people that will be able to run those scripts.

The importance of adoption holds true for the public resources and documentation as well. Thanks to its large community, Bash has quite a few more resources out there to help you learn how to use it.

So, if you are planning on writing a script that you want many developers to easily be able to run then I'd recommend that you go with Bash. However, this shouldn't stop you from using Z shell if your end goal is more suited to Z shell. Finding the right solution to a problem is much more important than just using what's popular, so keep that in mind as well.

Although Bash is much more popular, that doesn't mean Z shell is without any useful features of its own. It's actually heavily praised for its interactive use, because it's more customizable than Bash. For example, the prompts are more flexible. It can display a prompt on the left _and_ another on the right side of the screen, much like vim's split screen. The auto-completion is also more customizable and is actually faster than Bash's.

To give you a better sense of what kind of features Z shell has, here is a list of things that you will have access to when using Z shell over Bash:

- The built-in `zmv` command can help you do massive file/directory renames. e.g. to append '.txt' to every file name run `zmv –C '(*)(#q.)' '$1.txt'`
- The `zcalc` utility is a great command-line calculator which is a convenient way to do a quick calculation without leaving the terminal. Load it with `autoload -Uz zcalc` and run with `zcalc`
- The `zparseopts` command is a one-liner that lets you to parse complex options that are provided to your script
- The `autopushd` command helps you do `popd` after you use `cd` to go back to your previous directory
- Floating point support (which Bash suprisingly _does not_ have)
- Support for hash data structures

There are also a bunch of features that are present in the Bash terminal but are absent from almost all of the other shells. Here are a few of them as well:

- The `–norc` command-line option, which allows the user to proceed with the shell initialization _without_ reading the `bash.rc` file
- Using the option `–rcfile <filename>` with `bash` allows you to execute commands from the specified file
- Excellent [invocation features](https://www.gnu.org/software/bash/manual/html_node/Invoking-Bash.html)
- Can be invoked with the `sh` command
- Bash can be run in a specific POSIX mode. Use `set –o posix` to invoke the mode or `--posix` on startup
- You can control the look of the prompt in Bash. Setting the `PROMPT_COMMAND` variable to one or more of the [special characters](https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html) will customize it for you
- Bash can also be invoked as a [restricted shell](https://www.gnu.org/software/bash/manual/html_node/The-Restricted-Shell.html) (with `rbash` or `--restricted`), which means certain commands/actions are no longer allowed, such as:
  - Setting or unsetting the values of the `SHELL`, `PATH`, `ENV`, or `BASH_ENV` variables
  - Redirecting output using the `>`, `>|`, `<>`, `>&`, `&>`, and `>>` redirection operators
  - Parsing the value of SHELLOPTS from the shell environment at startup
  - Using the exec builtin to replace the shell with another command.
  - And many more...

It's difficult to say which shell is actually better. It all really depends on your own preferences and what you actually want to do with the shell. In the case of Bash vs Z shell, neither is really better than the other.

There are quite a few fans of the Z shell throughout the developer community who advocate for it quite heavily thanks to the many useful features it provides. On the other side, there are even more fans of Bash who know that their biggest advantage is their far larger user base. It's easy to see why it is so difficult to get developers to switch from Z shell to Bash and vice versa.

---

Thanks for reading! Check us out over at [StackAbuse.com](https://stackabuse.com/) for more articles, guides, and other resources. You can also follow us [@StackAbuse](https://twitter.com/stackabuse) for new articles daily.