Zsh vs Bash When we talk about UNIX-based programming we usually mean consoles, terminals and command line interfaces. The most common command shell is Bash, but there are other options available and widely used, such as the Z Shell or zsh.
In this article, Zsh vs Bash we will try to draw a line between the two shells and show the differences so that you can understand why you should use one or the other. But first, we will present each of them separately, before comparing them together.
Zsh, or Z Shell, was first released by Paul Falstad in 1990, when he was still a student at Princeton University. Z Shell is included in many operating systems, including Mac OS (although it is not installed there by default).
Like Bash, the Z Shell can be broadly thought of as an extended version of the Bourne Shell, and it contains many similarities to Bash, as you will notice below. You can also see that it is quite similar to the Korn Shell. Some features worth mentioning (but not all):
- File search patterns
- Correcting spelling
- Short directory names (e.g. ~ or ..)
- Loadable modules like socket control or FTP client
- Compatibility modes (for example, you can use
/bin/bashas a replacement for Bash)
- The scripts run on / off
- Completing git commands
- Expanding paths: for example, type
cd /u/lo/b, press Tab, and the path will be completed in the view
cd /usr/local/bin, since it is the only path that matches.
There are many more of them than we have shown here, but at least it makes us understand that the shells can be different.
The Bash shell (also known as the “Bourne-again shell” or “reborn shell”) was released around the same time as the Z Shell (1989), and is believed to have been created by Brian Fox (Brian Fox). It was originally written as a replacement for the Bourne Shell. Within a few years it became the main shell for GNU, most Linux distributions and Mac OS (version 10.3+). As befits a true successor, Bash is capable of executing all Bourne Shell commands without problems.
Here are some of the features that Bash has, including some of the lesser known ones:
- Insert the closing parameters of the previous command into the current one using
- You can continue executing the process even after exiting. To do this, use the command
disown -h <pid>, where
<pid>you need to specify the process number instead .
- Re-run the previous command by
sudo !!(!! – a designation for the previous command).
- Perform a reverse incremental search using the
- Press the Tab key twice and you will get a list of possible complements for the word you just entered or are typing.
- When you run a script in Bash, the -x option will print its contents as it did when it was run.
Comparing Zsh vs Bash
Now that we’ve briefly introduced both shells to you, let’s see how they behave in comparison.
The first thing we’ll look at (and this is one of the most significant aspects in my opinion) is the popularity of the shell. Although the Z Shell has a number of users in the development environment, it is generally safer to write your own scripts for Bash, as there are many more people able to run those scripts.
The importance of all this lies in the adaptation of scripts for public repositories, as well as in the ability to write competent documentation. Thanks to its large community for Bash, there are several great resources that can help you figure out how to use it.
So if you are going to write a script that is easy for many developers to run, then I recommend Bash to you. Although that shouldn’t stop you from using the Z Shell where it is more useful for your purposes. Finding the right solution to a problem is much more important than taking what’s popular, so keep that in mind.
While Bash is far more widespread, that doesn’t mean the Z Shell lacks useful features. It is often praised for its interactive experience as it is more customizable than Bash. For example, the command line is more flexible. It is possible to display a command on the left and another on the right, as in vim’s split screen. Auto-completion is also faster and more mutable than Bash.
To give you a better understanding of the Z Shell’s set of distinguishing features, here is a list of what you get using the Z Shell instead of Bash:
- The builtin command
zmvwill help you rename files / directories in bulk, for example to add ‘.txt’ to the name of each file, run
zmv –C '(*)(#q.)' '$1.txt'.
- The utility
zcalcis a wonderful command line calculator, it is a convenient way to read quickly without leaving the terminal. Download it via
autoload -Uz zcalcand run it with the command
zparseoptsis a one-liner that will help you parse the complex options that are presented to your script (?)
- The command
autopushdallows you to do
popdafter you use
cdto return to the previous directory.
- Floating point support (which Bash, surprisingly, does not).
- Support for hash data structures.
There are also a number of features that are present in Bash, but almost all other shells lack them. Here are also some of them:
- Command line option
–norcthat allows the user to deal with command line initialization without reading the file
- Using the
–rcfile <filename>c option
bashallows you to execute commands from a specific file.
- Excellent callability (set of command line options)
- Can be called by the command sh
- Bash can be run in a specific POSIX mode. Apply set –o posixto enable the mode, or ––posixat startup.
- You can control the appearance of the command line in Bash. Setting a variable PROMPT_COMMANDwith one or more special characters will set it up for you.
- Bash can also be enabled in restricted shell mode (with rbashor –restricted), which means that some commands/actions will no longer be available:
- Setting up and removal of service variables
- Redirecting output using operators ‘>’, ‘> |’, ‘<>’, ‘> &’, ‘&>’, ‘>>’
- Parsing SHELLOPTS values from the shell environment at startup
- Using the built-in exec statement to replace the shell with another command
- And much more
- Setting up and removal of service variables
Zsh vs Bash
It’s hard to say which shell is actually better. It depends on your personal preference and what you want to do with the shell. In the case of Z Shell and Bash, neither is better than the other.
There are a few Z Shell fans in the developer community who champion it because of the many useful things it has. But there are even more Bash lovers who know that its biggest advantage is its larger user base. It’s easy to see why it is so difficult for users to switch from the Z Shell to Bash and vice versa.