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
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.
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.
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 as valid. If you type in an incorrect case,
zsh will correct it upon tab completion or hitting enter.
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.
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:
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:
- Basic Command Line Usage
- Getting Started with Sass
- Choosing The Best Text Editor For Web Development
- Magento FAQ
- Web Design Blog
- WordPress FAQ
- Photoshop Retouching
- Some common use cases of Sass
- An Introduction to Version Control using Git (2018 Update)
- Choosing The Best Text Editor For Web Development
- An introduction to the htaccess file (2018 Update)
- Using node-sass to compile Sass files in an npm script
- How to get rid of the “You have mail” Unix message
- Designing a simple navigation bar with Bootstrap 4
- Using Surge for deploying static sites (2018 Update)
- WP Engine’s Staging Area has Revolutionized Our WordPress Development Process
If you are on OS X, chances are 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 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
The default path output should be
To change back, just use
chsh -s /bin/bash.
bash will also temporarily switch the shell for that session.
.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.
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.
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:
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.
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.
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.