Bash vs Zsh: A comparison of two command line shells (2019 Update)

zsh v bash

If you have ever used the command line on an *nix system (Unix-based or Unix-like), chances are you’ve used the bash shell. bash (an abbreviation for “Bourne-again shell”) is the default shell for many Unix-like operating systems.

Common examples of Unix-like operating systems include GNU/Linux and macOS. Although many other shells exist for the terminal, one of the most popular shells is zsh, or the “Z shell”.

While bash and zsh are both powerful shells and share many prominent features, they also possess distinctions that users may have different preferences for. This post will give an overview on the features of and comparison of bash vs zsh, how you can set both as your default shell, as well as some configuration suggestions.

A short history of bash and zsh

In 1971, the very first version of the Unix operating system came with the Thompson shell as its default shell. While lacking in scripting capabilities, this first shell established many of the standards for input/output redirection. With the release of Version 7 Unix in 1979, the new OS came distributed with the Bourne shell as its default shell.

Creation of bash

Later in 1989, Brian Fox created the Bash shell for the GNU Project as a viable software alternative for the Bourne shell. Bash introduced significant improvements over its predecessor. Most prominent of these improvements was its capacity as a scripting language.Users of bash could write their own programs for automating tasks.

These “shell” scripts were executable by typing the file name (typically with a .sh extension). For the most part, bash can support the majority of Bourne and C shell’s features in addition to its own unique additions including command-line editing and integer calculations.

Creation of zsh

Shortly after Bash emerged, a student at Princeton University created the Z shell or “zsh.” Hand in hand with this operating shell is a community-based, online platform called Oh My Zsh which features major extensions such as plug-ins and themes for zsh. As of June 2019, macOS Catalina announced that they would be adopting zsh as their default shell in replacement of bash—a major “win” for zsh enthusiasts.

best wordpress hosting

Bash vs zsh: Common features between bash and zsh

For the most part, bash vs zsh share many convenient features that qualify both as highly efficient shells.

The z-command

One highly useful feature that both share is the z command, which allows developers to essentially keep track of their directories. With the z command, you can navigate to a frequently/recently visited directory just by typing z , followed by the directory name. For example, say you have a directory newProject located at ~/src/2018/projects/newProject. Using this tool, you can simply type z newProject.

Auto-completion

Another practical aspect of both shells is their tab auto-complete functions. Within each shell, you can just type the command, followed by -, then hit tab. This action will then immediately display all of the available options for that command. You can then just tab through the available options, until you find the one you’re looking for. Both shells offer flexibility and customization to their command-line auto-completion.

Auto-correction

Additionally, a helpful utility for developers in both shells, for the most part, is the auto-correction. In the Z shell, if you make an innocent typo while writing a file location, for example, spell correction is built-in and will automatically detect the typo. While this is not custom to bash, you can easily make the necessary adjustments to allow auto-correction by using shopt commands cdspell and dirspell.

Color customization

Even in terms of visual appeal and organization, both shells accomplish the task fairly well. zsh will differentiate the two by adding color highlighting to directories. Additionally, the font-weight will be slightly bolder. This is extremely helpful to see what’s a file and what’s a directory. The exact colors used will depend on your terminal color settings. On the other hand, bash users can also tweak their interface to reflect their visual needs with the LS_colors variable.

While these are only some of the various tools offered by both shells, they provide a glimpse of how bash and zsh admittedly resemble each other. However, they both possess some distinctions that can create a preference for one over the other among developers.

bash-vs-zsh

Seeking web design services? about getting your online business off the ground.

Bash vs zsh: Reasons to consider switching between shells

The commands and overall interaction for bash vs zsh are essentially identical. Knowledge and familiarity with one shell can easily be translated into the other without tackling a steep learning curve. However, both shells do have their share of unique features that may prompt developers to declare a clear preference.

For one, zsh has the leverage of a powerful online community called Oh My Zsh. Oh My ZSH is one of the oldest and most popular options for managing zsh configurations. Offering over 250 plugins and 140 different themes supplied by the community, Oh My ZSH is a great place to start in customizing the z shell that even comes with an auto-update function that keeps your shell updated. This allows users to work in a shell with a more personalized interface, amp up their workspace with a diverse array of tools, and have access to a convenient out-of-the-box support system. For instance, a highly favorable option for the interface is a right-handed side prompt or a side prompt that auto-hides when typing in long file paths or commands. Even this minute level of flexibility turns developers’ heads towards zsh.

On the other hand, Bash has an impressive set of programming functions such as looping and conditional constructs, tilde and brace expansions, and the use of aliases. It also has its share of quirks such as powerful invocation tools, being set into a restricted shell mode, having a particular POSIX mode, and more. Because Bash is also an older and established shell, there are boundless online resources for this software as well.

bash-vs-zsh

Installing zsh

If you are on OS X, chances are you already have zsh installed on your system. To check, run the following command:

which zsh

 

If it is already installed, you should receive an output message that states /bin/zsh as the file path for the shell. There is an off-chance that it may display another file path, if for some reason you moved the default installation directory. If you receive an output that states zsh not found, you will need to install it via Homebrew. You can do so by running the following command:

brew install zsh

 

For CentOS, Redhat, and Fedora Linux distributions, you can install zsh by running yum install zsh.

Similarly, sudo apt-get install zsh will install the shell on Debian and Ubuntu distributions. You can start using zsh even if bash is set to as your default shell. To temporarily try it out, you can just run zsh on the command line. This will temporarily switch the shell for your current session. To get the most out zsh, it’s also highly recommended to upgrade your terminal instead of using the default.

Terminal is the default on Mac OS X. While it works perfectly fine, options such as iTerm 2 or Hyper offer an enormous amount of customization. I personally use iTerm 2, although Hyper is an excellent choice available cross-platform.

Setting zsh as your default shell

Now that you have zsh installed, all you have to do to set it as your default shell is run the following command:

chsh -s /bin/zsh

To start using zsh, just close your terminal window and open a new one — you will now be running zsh instead of bash.

Returning to bash

If for some reason you’d like to return to bash as your default shell, first find out where bash is installed with which bash.

The default path output should be /bin/bash.

To change back, just use chsh -s /bin/bash.

Running bash will also temporarily switch the shell for that session.

Configuring zsh

Like bash’s .bashrc, zsh uses a dot file to store user configuration settings, .zshrc. Stored in the home directory (~/.zshrc). By default there won’t be a configuration file setup, so you’ll need to create one in your home directory (~/.zshrc).

Similarly, you can add a .zprofile file to your home directory. .zprofile is a script that will run upon login. You can add in any commands you’d like to run upon starting up a new instance of the shell. Given the wide spectrum of customization available in zsh, it can be beneficial to use a dedicate configuration manager. Numerous options exist, including Prezto, Oh My ZSH, and Antigen.

Oh My ZSH

To install Oh My ZSH, simply run

curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh

 

This will download the executable shell script and install it on your system. The default settings upon installing Oh My ZSH are a great place to start.

By default, it will install the ‘robbyrussell’ theme (its namesake coming from the original developer of the project).

Additionally, it will install a number of useful plugins. At least a few aspects of the terminal’s display will appear immediately different.

Git branches

Similar to the above, Oh My ZSH also adds a plugin that will display the current branch you are on in a git project. Appearing after the working directory in the command prompt, you’ll always know which branch you’re on. This way you never have to git branch to know the current branch. The git plugin also adds in several other useful features.

In the example below, the project directory is currently on the tests branch. We can also know that the working tree is clean, with no changes that you haven’t committed yet. In this case, this is indicated by the fact that the display is green. As soon as you make a change, the display turns another color, indicating that there are new changes to commit:

bash-vs-zsh

zsh significantly improves the Git workflow with these types of indications right in the command prompt.

Changing your theme

As mentioned above, zsh comes with hundreds of different themes to choose from. robbyrussell will be the default theme, although you can easily change this. To find a new theme offered by Oh My ZSH, you can preview each on the Themes page in the project repo. Given the many choices available, chances are you’ll find one available with almost exactly the features you’re looking for.

Once you find one you like, open up your .zshrc file, in the home directory. Then, you’ll just need to update the line starting with ZSH_THEME. Put the name of the name you want to use (inside of quotes), then save the file. Opening a new terminal session should now display the new theme.

bash-vs-zsh

You can even set random as your ZSH_THEME. This will load a new random theme each time you open up a new session in the shell.

Some themes may require additional packages or fonts, such as the popular Powerline fonts. The theme’s installation’s instructions should specify any additional required packages, though.

Conclusion

As we’ve looked at in this zsh vs bash post, zsh and bash both share a similar and impressive array of features, but also have their own distinct tools as well. If you’re looking for extensibility, greater customization and advanced features not found in bash, the zsh shell combined with Oh My Zsh framework is an excellent choice. Regardless of what shell you choose, improved fluency on the command line provides countless benefits in efficiency, automation, and better understanding of your computer’s inner workings.

For an introduction to some of the most essential shell commands, check out our post on basic command line usage.

best wordpress hosting service

Resources

32 Comments

  • msi September 28, 2018 at 7:58 pm

    “bash (an abbreviation for “Bourne-again shell”) is the default shell for Unix-based operating systems…”

    This is not true. Bash is the default interactive shell on most Linux distributions and macOS, yes. It is, however, not the default – or even included in the base system – on any BSD variant I’ve come across. Commerical Unix systems, e.g. AIX, don’t use Bash either.

    Also, Linux is not Unix-based (based on original Unix code), but Unix-like (designed after the Unix way of doing things).

    • Nicholas Morera October 13, 2018 at 9:33 pm

      Thanks for pointing that out. The post has been updated to reflect this.

  • John F October 25, 2018 at 3:11 pm

    A lot of the benefits of zsh mentioned here are also available for bash via bash-it https://github.com/Bash-it/bash-it

    • Nicholas Morera October 25, 2018 at 6:46 pm

      This is true, although as the README.md notes, it’s a “shameless ripoff of oh-my-zsh” 🙂

  • bluz71 December 12, 2018 at 3:33 am

    Note, Bash also has some of the features listed above (when configured):

    – The ‘z’ utility is shell agnostic, it works on Bash and Zsh. I use it all the time in Bash. See here https://github.com/rupa/z/

    – With bash-completion installed and inputrc configured appropriately it is possible to setup Bash to have Zsh AUTO_MENU like completion (including option cycling).

    – The `cdspell` and `dirspell` options will do auto filesystem name correction in Bash

    – Directories and files can be configured in different colors via LS_COLORS variable in Bash

    – Command-click URLs are a terminal capability. In iTerm2 I can do COMMAND+CLICK on a URL and launch a browser.

    I discuss many of those Bash capabilities and settings here: https://bluz71.github.io/2018/03/15/bash-shell-tweaks-tips.html

    Bash can do 90% of what Zsh can do, it just requires some tweaks.

    Zsh does have some nice capabilities that Bash does not have, those being:

    – Right-hand side prompt (if you want it)

    – Command syntax highlighting

    – Descriptive tab completions (man page derived)

    Cheers.

    • pawamoy May 10, 2019 at 4:42 pm

      Thank you. I was reading the article and saying to myself: “This is false/incorrect/irrelevant”. I would like the author to update the post according to your comment. Otherwise it simply shows that they don’t know much about shells…

    • Crystal Lee June 21, 2019 at 8:15 pm

      Thank you for your constructive feedback on this post. The post has been updated to reflect your comment.

  • ry__ December 21, 2018 at 3:20 pm

    “…unless you type pwd”, not quite true. Take a quick look through the man page for bash/ksh and search (/ like vi) for PROMPTING. Here you’ll find how to set up your prompts (PS1 is the primary and there are others for different uses PS2..4). Specifically to put he info you’d like – /w gives the path.

    However, the scope for customization it is quite limited when compared to OMZ.

    • Stephen June 4, 2019 at 1:56 am

      “…unless you type pwd”, not true at all! Most Linux distros and (I’m pretty sure) macOS all come with PS1 set to something useful that includes the cwd. Typing `pwd` every time you need to see where you are is not an effective workflow and next to nobody does that.

      This article is more a comparison of the default configuration in bash and zsh rather than the features of the shells themselves.

  • minime February 14, 2019 at 1:40 pm

    That click and open thing is a function of your terminal emulator, not zsh. Also option completion as well as the z command are plugins for zsh, which are in similar form available for bash too. colour higlighting of files is built into bash as well.

  • shreyas April 1, 2019 at 6:34 am

    Why am I able to edit this blog?

    • Angelo Frisina April 9, 2019 at 8:16 pm

      How are you able to edit the blog?

  • Phat Tran April 3, 2019 at 9:06 am

    thanks for the post, i love `z` command very much, didn’t know of this plugin!
    btw, you’re setting contenteditable=”true” to your div(s), but not the titles like `h2`

    • Angelo Frisina April 9, 2019 at 8:25 pm

      thanks for the feedback Phat Tran.

  • Trix Farrar April 29, 2019 at 12:34 pm

    “With the release of Version 7 Unix in 1979, the new OS came distributed with bash as its default shell.”

    This statement is not correct.

    The default shell in Version 7 (and subsequent UNIX-branded operating systems) is the Bourne Shell.

    bash was created in 1989, by Brian Fox, as part of the GNU project. (https://en.wikipedia.org/wiki/Bash_(Unix_shell))

    • Crystal Lee June 21, 2019 at 8:16 pm

      Thank you for pointing out this error, Trix Farrar. The post has been updated to reflect your comment.

  • Jim June 4, 2019 at 12:35 pm

    Okay, tried it, it barfed on every line of my .bash_profile. Not so very compatible I guess.

    Also I’m pretty sure /bin/sh was just as scriptable since I used it for 15 years. Well, that an ksh.

  • Pete June 4, 2019 at 4:20 pm

    Nice guide! This entry is about to blow up due to Apple’s announcement of the switch to ZSH in Catalina. 🙂

    • Crystal Lee June 21, 2019 at 8:22 pm

      Thank you for the positive feedback, Pete! The post has recently been updated to also reflect this new announcement regarding macOS Catalina.

  • Bruno June 4, 2019 at 7:35 pm

    What’s the theme used in the examples?

  • Chris June 4, 2019 at 11:59 pm

    This is a nice article. However, there are only a couple of zsh features listed here that are truly unavailable in bash (unless you are using macOS’s ancient version of bash).

    For example, tab auto-completion works in bash. You do not, as the article states, know the full pathway for a cd command. All you have to know are partial directory and file names if you are willing to tab through the levels you don’t know exactly.

    It’s also misleading to say that bash won’t show the current working directory in the prompt (by default, neither does zsh, but both are easily amended to display that information) or that bash doesn’t play well with git, as there are packages for that.

    I’m not saying these things are equivalent to their zsh counterparts. They aren’t, and many people like the zsh variants better. What I am saying is that I found the article less compelling because the bash side of the conversation felt like a straw man. Maybe you could have run this past a fan of bash before printing it so that they could have helped you root out the misleading bits.

    On the other hand, there are some things zsh does that are truly not part of bash, that you did not even touch on, like recursive path expansion that allows you to type “cd /u/sh/cal” and have it auto-expand into “cd /usr/share/calendar.”

    And the standout feature of zsh seems to be its builtin level of customizability. For example, if you run something like “set -o | grep ‘on|off’ | wc”, you will find that there are 179 shell options exposed in zsh compared to only 27 in bash.

    Like I said, good stuff. Next time, maybe run your stuff past a bash enthusiast before saying what bash can’t do.

    • Crystal Lee June 21, 2019 at 8:18 pm

      Thank you Chris for your helpful insight on bash and sharing your concerns. The post has been updated to present bash and zsh more accurately based on your comment.

  • Mike Maxwell June 5, 2019 at 1:53 am

    Something I find very useful, is to set stderr to be red, and stdout black. There are of course ways to do this in bash (and I assume in zsh) for a single command, but doing it for a session seems to be harder. I have a perl script that starts at startup that does this , but that seems like using a sledge hammer to kill a fly. There are some other solutions here: https://unix.stackexchange.com/questions/12439/can-i-configure-my-shell-to-print-stderr-and-stdout-in-different-colors. But I’m surprised this doesn’t come standard with some shell or other (I came here hoping it came with zsh…).

  • Steve Simmons June 5, 2019 at 7:27 pm

    The legacy Unix shell was written by Steve Bourne, not Ken Thompson – hence bash being called the ‘Bourne-again’ shell.

  • Peter Birch June 6, 2019 at 1:59 am

    Your short history of bash is incorrect. The shell that replaced the Thompson shell in 1979 is the Bourne Shell (sh), not BASH. The original Bourne Shell (sh) is not the same as BASH. BASH did not arrive until 1989 with the GNU Project. (https://en.wikipedia.org/wiki/Bourne_shell)

    • Crystal Lee June 21, 2019 at 8:19 pm

      Thank you for pointing out this error, Peter Birch. The post has been updated to reflect your comment.

  • badbanana June 6, 2019 at 3:28 am

    except for the “z filepath” command, everything else is available in bash.

    wake me up when there’s a real replacement for bash.

  • Robert Eksten June 6, 2019 at 7:29 pm

    Several mistakes in your history lesson.

    Bash was certainly not released in 1977. As others have noted, you’ve confused Bash and the Bourne shell (not the same thing). It was Bourne shell that was released with Research UNIX. Bash was never mentioned in The UNIX Programming Environment; that book was published 5 years before Bash was even coded.

    Note about security:

    What is your assertion that Zsh offers better security based upon? As you mentioned Bash had an undiscovered vulnerability for years, but what makes you think Zsh doesn’t have undiscovered security vulnerabilities? I don’t think you’ve offered any valid reasons why Zsh would be more secure than Bash.

    • Crystal Lee June 21, 2019 at 8:20 pm

      Thank you for pointing out this error and sharing your concerns with the post, Robert Eksten. The post has been updated to reflect your comment.

  • Kate Ebneter June 7, 2019 at 12:08 am

    Bash definitely was not released with Version 7 Unix. Bash is a GNU project and was first released in 1989.

    • Crystal Lee June 21, 2019 at 8:21 pm

      Thank you for pointing out this error, Kate Ebneter. The post has been updated to reflect your comment.

  • Ayaz June 9, 2019 at 10:27 pm

    What is the color theme used for powerlevel9k?

Post a Comment

Comments are moderated. Your email is kept private. Required fields are marked *

© 2019 Sunlight Media LLC