If you have ever used the command line on a *nix system (Unix-based or Unix-like), you’ve probably 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 powerful shells and share many prominent features, they also possess distinctions for which users may have different preferences. This post will overview the features and comparison of bash vs. zsh, how to set both as your default shell, and 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 input/output redirection standards. 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. The most prominent of these improvements was its capacity as a scripting language, and bash users could write their 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 most of Bourne and C shell’s features and unique branches, 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 significant extensions such as plug-ins and themes for zsh. As of June 2019, macOS Catalina announced they would adopt zsh as their default shell to replace bash—a significant “win” for zsh enthusiasts.
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.
One handy feature that both shares are the z command, which allows developers to keep track of their directories. With the z command, you can navigate to a frequently/recently visited directory 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 type z newProject.
Another practical aspect of both shells is their tab auto-complete functions. You can type the command within each shell, followed by -, then hit the tab. This action will then immediately display all available options for that command. You can then tab through the options until you find the one you want. Both shells offer flexibility and customization to their command-line auto-completion.
Additionally, a helpful utility for a web design company in both shells, for the most part, is auto-correction. In the Z shell, if you make an innocent typo while writing a file location, 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.
Even in terms of visual appeal and organization, both shells accomplish the task reasonably 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 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: 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 another without tackling a steep learning curve. However, both shells have unique features that may prompt developers to declare a clear preference.
For one, zsh has the leverage of a robust 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 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 various tools, and access 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 toward 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 unlimited online resources for this software.
If you are on OS X, you already have zsh installed on your system. To check, run the following command:
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 must 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 your default shell. To temporarily try it out, run zsh on the command line. This will temporarily switch the shell for your current session. To get the most out of zsh, upgrading your terminal is highly recommended instead of using the default.
The terminal is the default on Mac OS X. While it works perfectly fine, options such as iTerm 2> or Hyper offer enormous customization. I 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, 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, use chsh -s /bin/bash.
Running bash will also temporarily switch the shell for that session.
Like bash’s .bashrc, zsh uses a dot file to store user configuration settings, .zshrc. Held in the home directory (~/.zshrc). There won’t be a configuration file setup by default, so you must 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 any commands you’d like to run upon starting a new shell instance. Given the broad spectrum of customization available in zsh, using a dedicated configuration manager can be beneficial. Numerous options exist, including Prezto, >Oh My ZSH, and Antigen.
Oh My ZSH
To install Oh My ZSH, 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. Upon installing Oh My ZSH, the default settings are a great place to start.
By default, it will install the ‘robbyrussell’ theme (its namesake coming from the project’s original developer).
Additionally, it will install several useful plugins. At least a few aspects of the terminal’s display will appear immediately different.
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 the branch to know the current one. The git plugin also adds several other valuable 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 you haven’t committed. In this case, this is indicated by the fact that the display is green. As soon as you make a change, the show turns another color, meaning that there are new changes to commit:
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. robbyrussell will be the default theme, although you can easily change this. To find a new theme offered by Oh My ZSH, preview each on the Themes page in the project repo. Given the many choices available, chances are you’ll find one available with almost precisely the features you’re looking for.
Once you find one you like, open up your .zshrc file in the home directory. Then, you’ll 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.
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, such as the popular Powerline fonts, may require additional packages or fonts. The theme’s installation instructions should specify any other packages needed, though.
As we’ve looked at in this zsh vs. bash post, zsh and bash share a similar and impressive array of features but also have their specific tools. 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 a better understanding of your computer’s inner workings.
For an introduction to some of the essential shell commands, check out our post on basic command line usage.
Angelo Frisina is a highly experienced author and digital marketing expert with over two decades of experience in the field. He specializes in web design, app development, SEO, and blockchain technologies.
Angelo’s extensive knowledge of these areas has led to the creation of several numerous award winning websites and mobile applications, as well as the implementation of effective digital marketing strategies for a wide range of clients.
Angelo is also a respected consultant, sharing his insights and expertise through various podcasts and online digital marketing resources.
With a passion for staying up-to-date with the latest trends and developments in the digital world, Angelo is a valuable asset to any organization looking to stay ahead in the digital landscape.