Bash vs Zsh: A comparison of two command line shells (2018 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 is a perfectly functional shell, there are many valid reasons for switching to zsh. Some of the improvements offered by zsh include security, auto-completion, and many other features. This post will give an overview on the benefits of zsh, how you can set it as your default shell, as well as some configuration suggestions.

A short history of bash

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 bash as its default shell. Intended as a replacement for the Thompson 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).

Aside from the significant power bash offered its users, its popularity can be partly attributed to other factors. The monumental book, The Unix Programming Environment, prominently featured the bash shell. First published in 1984, this book was widely-influential espousing the tenets of the Unix philosophy. Dozens of different shells have been released since bash’s first release in 1977. Each of these new shells have offered a variety of different improvements and deviations from their precursors. One such of these (relative) newcomers is the Z shell, first released in 1990.

best wordpress hosting

Reasons to consider switching from bash to zsh

The commands and overall interaction for bash and zsh are essentially identical. However, zsh includes many useful features for both the beginner and advanced CLI user not available in bash.

Auto-completion and “frecent” directories

zsh offers far superior auto-completion features for file paths, and popular applications, such as git. This not only saves time on typing but can help out when you’re not exactly sure on the right command or file path.

The biggest advance in auto-completion that zsh offers is via the z command. To change directories in bash, you have to type cd followed by the full file path (relative to your current working directory). This can be tedious and requires knowing the full pathway for where you’re trying to get to. 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. If you were in the home directory, you’d have to type cd src/2018/projects/newProject in bash. In zsh, you can simply type z newProject. Even better, the directory does not have to be a subdirectory within your current path. You can be anywhere on your system, and z newProject will work the same way. This is a huge time saver, not requiring you to memorize the full file path for any commonly visited directories.

You should note that you have to navigate to a directory the “normal” way via cd at least once. After that z will commit this file path to “memory”. After that you can just use the z command thereafter.

Command option display

Similar to auto-complete, there is a feature available for command options. Most commands have numerous options available, designated with flags afters the main command (such as ls -l or ls -a). Unless you have the options memorized, you’d typically have to open up the man pages or search online. With zsh, you can just type the command, followed by -, then hit tab. zsh will then immediately display all of the available options for that command, along with a short description. You can then just tab through the available options, until you find the one you’re looking for.

bash-vs-zsh

Typing error correction

Another major benefit of zsh is error correction when you make a typo. Rather than just display error: command not found, zsh will try to interpret what you tried to type. zsh will accept this input as a valid command.

Related to error correction, the Z shell is also case insensitive. In bash, the case of a file or directory needs to be exact in order for it to recognize the command. Z shell is case insensitive. So for example, typing dropbox in bash would result in an error. zsh would treat either dropbox or Dropbox as valid. If you type in an incorrect case, zsh will correct it upon tab completion or hitting enter.

Security

Another major benefit of zsh is better security over bash. In 2014, bash was found to have some security vulnerabilities existing from version 1.03, released as far back as 1989. While most computers will have patches installed to address the bugs, not all systems have addressed the security issues. Systems most at risk would be servers with remote access via superuser.

Color highlighting

It can be quite difficult to differentiate between files and directories in bash. This is due to both appearing exactly the same visually. 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:

bash-vs-zsh

Command-click URLs

In bash, when a URL appears somewhere that you want to visit, you’d normally have to highlight the URL with your mouse, then copy/paste it in order to open in your browser. With zsh, you can just hit the command key, then click on the URL. This will then immediately open up the URL in your default browser:

Resources.

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

Oh My ZSH is one of the oldest and most popular options for managing zsh configurations. Offering over 200 plugins and 140 different themes supplied by the community, Oh My ZSH is a great place to start in customizing the z shell.

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.

Display current directory

One annoying aspect of bash is that you never know what directory you’re in unless you type pwd (print working directory). Oh My ZSH solves this with a handy plugin that will add the working directory to the command prompt. This is very handy for quicker navigation and knowing exactly where you are in the file system.

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 post, zsh offers numerous benefits over bash, with little to no drawbacks. If you’re looking for extensibility, customization and advanced features not found in bash, zsh 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

11 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.

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

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

Post a Comment

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

© 2019 Sunlight Media LLC