The Complete Magazine on Open Source

Zsh: The Ultimate Alternative to Bash

, / 224 0
A shell is the interface to the operating system and the services provided by it. The default shell on any operating system is the bash shell. However, there are more and better alternatives to the bash shell. Zsh is one of them.

The terms terminal, tty, console and shell are used synonymously. There is nothing wrong with that but these words mean a lot of different things. They represent pieces of hardware that were used to enter data into the computer in earlier times, as the machines back then used to be very big and data entry was done using terminals, which were also known as teletypewriters (hence, tty). Physically, they were known as consoles (from the furniture point of view). The terminal (console) in our computers these days is an application which is similar to the terminal of past days but it doesn’t run on a big machine; instead, it runs on the top of the kernel in our machine. Coming to shell, it is a command interpreter that makes the use of the terminal very easy by interpreting certain characters in a specified manner—for example, tab completion in bash, which was absent in the older shells. In the same way, bash lacks some features which are covered by zsh.
Figure 1 shows that the shell works as an interpreter.

Figur 1

Figure 1 : How the shell works

Different types of shell

Sh: The Bourne shell, called sh, is one of the original shells, developed for UNIX computers by Stephen Bourne at AT&T’s Bell Labs in 1977. Its long history of use means many software developers are familiar with it. It offers features such as input and output redirection, shell scripting with string and integer variables, and condition testing and looping.

Bash: The popularity of sh motivated programmers to develop a shell that was compatible with it, but with several enhancements. Linux systems still offer the sh shell, but bash (the Bourne-again shell), which is based on sh, has become the new default standard. One attractive feature of bash is its ability to run sh shell scripts, unchanged. Shell scripts are complex sets of commands that automate programming and maintenance chores; being able to reuse these scripts saves programmers time. Conveniences not available with the original Bourne shell include command completion and a command history.

Csh and Tcsh: Developers have written large parts of the Linux operating system in the C and C++ languages. Using the C syntax as a model, Bill Joy at Berkeley University developed the C-shell (csh) in 1978. Ken Greer, working at Carnegie-Mellon University, took csh concepts a step forward with a new shell called tcsh, which Linux systems now offer. Tcsh fixed problems in csh and added command completion, in which the shell makes educated guesses as you type, based on your system’s directory structure and files. Tcsh does not run bash scripts, as the two have substantial differences.

Ksh: David Korn developed the Korn shell, or ksh, about the time tcsh was introduced. Ksh is compatible with sh and bash. Ksh improves on the Bourne shell by adding floating-point arithmetic, job control, command aliasing and command completion. AT&T held proprietary rights to ksh until 2000, when it became open source.

Zsh: The Z shell is the ultimate for feature-hungry users. If you can think of something that a shell ought to do, the Z shell probably does it. All that power comes at a price. For example, the zsh executable is nearly four times larger than the ksh executable and almost three times larger than the sh executable (Bourne shell). Zsh was developed by Paul Falstad.

If you want to know which shell is installed in your computer, use the following command:

$ ls /bin/ | grep sh

You can try all the shells available in your computer by just typing the name of the shell in the terminal.
Note: There are many other shells out there such as fish (the friendly interactive shell), POSIX, Ash, etc. I have given a brief on the ones that everyone knows a little about.

Figure 2

Figure 2 : Different shells

Why bash?
By default, we have the bash shell for users and the Bourne shell (sh) for the root user in Ubuntu, which has been the default since version 6.10. Fedora (Red Hat) also uses bash as the default shell. The major reason to switch the default shell is the efficiency of the bash. One day, every distro will have to shift to zsh as it is more efficient than bash.
The main features that have made bash the default shell in most distributions are:

  • bash implements command aliases, and the alias and unalias built-ins
  • bash includes a built-in help for quick reference to shell facilities
  • bash can be customised easily by using the ~/.bashrc file
  • bash has process substitution
  • bash has indirect variable expansion using ${!word}
  • bash implements csh-like history expansion

You can get a list of all the major differences from the following link:

Shell scripting
These are the rules that must not be forgotten while writing a script:

  • The first line should be #!/bin/sh
  • You must change the permissions of the file to execute it —chmod a+x filename
  • Always use comments to explain the code; if the first character is ‘#’ , then the line is treated as a comment.
  • Use the echo command to explain what is happening in the background when the shell is running.
  • The extension of the file need not be .sh

Here’s my first shell code:

#! /bin/sh
# my first script

echo “Hello World”

The first line says that this is a code that needs to be executed using the Bourne shell. The second line is a comment as it starts with #. Echo just prints whatever exists within the “” ”

#! /bin/sh
# greeting script

echo “Hi $USER”
echo “Have a nice day !!!!!!!!!”

Echo substitutes the $USER with the username logged in and prints the greetings.

#! /bin/sh
# greeting with reading name

echo “Hi $USER”
echo “Please enter your real name”
read a;
echo “Thank you for entering your name”
echo “Have a nice day $a !!!!”

exit 0

The read takes the input and saves it in the variable a. And the echo command prints the name by substituting it in the $a. Exit just exits from the program.
In bash, you can use syntax that is similar to C (programming language). Here is a script which prints all the files that are executable in the given directory.
Note: It also returns the directories (folders) because the folders also have the executable permission.

# find all executables
# Test arguments
if [ $# -ne 1 ] ; then
echo “Usage is $0 <dir>”
exit 1
# Ensure argument is a directory
if [ ! -d “$1” ] ; then
echo “$1 is not a directory.”
exit 1
# Iterate the directory, emit executable files
for filename in “$1”/*
if [ -x “$filename” ] ; then
echo $filename
echo “$count executable files found.”
exit 0

In this script, the thing that is newly added is the if statement. As you can see, the syntax is a bit different and you should also use then after the if. Besides, the brackets used for if are a bit different.

How to install zsh
Installing zsh is very easy. It can be done from your package manager based on your distribution, using the following command, which you can also use to update the zsh if you had already installed it.
For Ubuntu- or Debian-based distros, type:

sudo apt-get install zsh

For Red Hat-based distros, type:

sudo yum install zsh

For SUSE-based distros, type:

sudo zypper install zsh

For Arch Linux or Manjaro, type:

sudo pacman -S zsh

You can try using the zsh shell by just typing zsh in a terminal. If you are using zsh for the first time, you will get four options with a heading zsh-newuser-install.

Now type ‘0’ and it goes to a basic zsh which looks like it lacks features in comparison to bash. But don’t uninstall zsh and shift back to the old bash as there is more to be installed. You can modify zsh to fit your needs by modifying the .zshrc file, using the following command:

vi ~/.zshrc
Figure 3

Figure 3 : zsh-newuser-tall

Note: You can use whichever editor you want instead of Vim to edit the .zshrc file, though I prefer Vim. We can skip this and use a readymade zsh configuration such as oh-my-zsh.

Installing oh-my-zsh
oh-my-zsh is an open source, community-driven framework for managing your zsh configuration. It comes with lots of helpful functions, helpers, plugins, themes, and a few things that make zsh more user friendly. You can find more about it from its website and you can contribute to it at It can be installed in two ways, either by using Curl or wget. oh-my-zsh uses Git, so you need to first install some packages in order to be able to install oh-my-zsh. Here’s a list of them:

sudo apt-get install git-core curl

If you are using Curl, type:

curl -Lhttp :// | sh

If you are using wget, type:

wget --no-check-certificate -O - | sh
Figure 4

Figure 4 : zsh logo

Figure 5

Figure 5 : Installing zsh using wget

Figure 5 shows how the installation looks on a Ubuntu 14.04 system.
If we use the above two commands, oh-my-zsh installs itself automatically. You can install it using git in the following manner:

p ~/git clone git:// ~/.oh-my-zsh
cp ~/.zshrc ~/.zshrc.old
cp oh-my-zsh .o.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc

Setting zsh as the default shell
By default, bash is set as the shell in most modern distributions. You can change the default shell by using the chsh command:

chsh -s /bin/zsh username

Here, username is the one with which you are logged in. You can do the same with the root user but add sudo in front of it, as changing the shell for the root requires more privileges.
Note: By default, oh-my-zsh is installed only for the user.

By default, oh-my-zsh is set to its own theme, but you can set it to whatever you want to by changing line 10 of ~/.zshrc to random or the name of the zsh-theme available on ~/.oh-my-zsh/themes/. You can also choose your favourite themes by running the following commands at a terminal. There are a lot of themes available, and each has a different purpose — some show the time, others show the battery level, etc.

cd ~/.oh-my-zsh/tools/

After typing the above command, you will be shown a cool heading as the zsh theme chooser, which displays the preview of all the themes available in the folder ~/.oh-my-zsh/themes/ and asks whether to add the given template to your favourites list. There are also many tools available in the tools directory of .oh-my-zsh.
Updating oh-my-zsh
Updating oh-my-zsh is very easy. It can be done by typing upgrade_oh_my_zsh on the command line.
Why is zsh better than bash?
Here are a few reasons.
It’s similar to bash: The foremost plus point of zsh is that it is backward-compatible with bash. Every valid command in bash becomes a valid command of zsh (except for a very few exceptions). Even if the zsh was more powerful than bash, it would have not been used by any one if it had some new and weird syntax. So if you know how to use a normal command line (bash), then you know the most powerful shell (zsh) syntax.
Spelling mistakes: It corrects almost all the spelling mistakes when you’re using the command line. For example, in bash, if you type cd /deskt and press the tab, it doesn’t give us anything, but in zsh, it changes itself to cd /Desktop.
Intelligent tab completion: The tab completion doesn’t just complete the last command but also checks for the whole line. For example, if you type cd /u/lo/b and hit a tab, it changes to cd /usr/local/bin/. Similarly, when you type cd /u/l it expands till cd /usr/l, before asking you if you mean lib or local, as there is a chance of either being the right choice.
Command history: This is one of the most important features. In bash, if we type some command and press the up arrow a number of times, we get the whole history. But in zsh, if we type a command and use the arrow button, we only get the commands that have the specific command in it. As an example, the order in which we typed the commands is shown below:

cat /etc/passwd
cd ~
cd /root

In bash, if we type ‘cat’ and then the arrow key, we get cd /root but in zsh, we go directly to the cat /etc/passwd as zsh gives the history of the specific command.
Sharing history across sessions: If you are running two different zsh sessions, whatever command we typed last can be shared with any of the other terminals with zsh. This can’t be done in bash.
Extended globbing: Globbing means the use of ‘*’ to match all the items—in zsh, we can use ‘**’ to match even the directory that is located very deep.
Easy scripting: With zsh, we can do shell scripting very easily because it has a more tolerable syntax. (Even though I don’t recommend using it as it is not installed in every machine by default. But it can be used locally for daily work if one is too lazy to type a particular instruction every day).
Aliases: By default, zsh has many aliases, which are mainly typing errors.
Many other features: Zsh supports many other features such as git completion, the built-in less command, etc. zsh, along with the configuration of oh-my-zsh, turns into a powerful tool and makes the command line interface more friendly.
Note: Some themes in oh-my-zsh may not work in a proper manner. So you can report the problems at

Figur 6

Figure 6 : Upgrading oh-my-zsh

Uninstalling zsh/oh-my-zsh
Zsh is a very good shell that you may not be interested in uninstalling. In case you don’t like the oh-my-zsh configuration, you can uninstall it using the simple command shown below:


This removes the oh-my-zsh and restores the .zshrc which was configured before installing oh-my-zsh. If you don’t want zsh at all, then you can run the following command:

sudo apt-get remove zsh
sudo apt-get remove --purge zsh

Note: If even after using the above commands the configuration files in .zshrc do not get removed, you need to remove them manually by using rm -rf ~/.zshrc, or by using Ctrl+H (to show the hidden files in Nautilus) and pressing Shift + Delete (to delete them permanently without saving them to Trash).