Zsh vs Bash

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 but there are other variants available and used widely as well, like Zsh or the 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, 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 KornShell as well. Some of the features that are worth mentioning include (but are not limited to):

  • File globbing
  • 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.

Comparing Z shell and Bash

Now that we've given 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 that of Bash.

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 filename 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 surprisingly 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:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

  • 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
  • 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 will customize it for you
  • Bash can also be invoked as a restricted shell (with rbash or --restricted), which means certain commands/actions are no longer allowed, such as:
    • Setting or un-setting 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.

Learn More

So where do you go from here? There are a lot of details to master when learning to write shell scripts. If you want to learn more, I'd suggest taking a course like the BASH Programming Course: Master the Linux Command Line, which will teach you just about everything you need to know about scripting (specifically in Bash), starting with beginner concepts and working its way up to advanced topics.

Hopefully now you have an idea as to how both Bash and Z shell differ, as well as their similarities. Quite a few resources exist out there for both shells, so you won't have any trouble finding more information if you need it.

Which shell do you prefer, and why? Let us know in the comments section!

Last Updated: July 10th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Make Clarity from Data - Quickly Learn Data Visualization with Python

Learn the landscape of Data Visualization tools in Python - work with Seaborn, Plotly, and Bokeh, and excel in Matplotlib!

From simple plot types to ridge plots, surface plots and spectrograms - understand your data and learn to draw conclusions from it.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms