When we talk about UNIX based programming, it's usually about the shells, terminals, and the command line interfaces. The most prevalent shell in this regard is Bash but there are other variants available and used widely as well, like Zsh or the Z shell.
In this article, we'll attempt to draw the line between the two shells and show the differences so you can get a sense of why you might use one or the other. But first, in the following sections we'll introduce both shells before we compare them together.
Zsh, or Z shell, was first released by Paul Falstad back in 1990 when he was still a student at Princeton University. Z shell is included in many operating systems, including Mac OS (although it isn't the default that's actually used).
Much like Bash, Z shell can basically be seen as an extended version of the Bourne shell, and does contain a lot of the same features as Bash, which you'll probably notice in the sections below. You may also notice that it pretty closely resembles the Korn shell as well. Some of the features that are worth mentioning include (but are not limited to):
- File globbing
- Spelling correction
- Directory aliases (much like
- Loadable modules, like socket controls or an FTP client
- Compatibility modes: e.g. You can use
/bin/bashas a drop-in replacement for Bash
- Startup/shutdown scripts via
- Path expansion: e.g. Enter
cd /u/lo/b, press tab, and it will be completed to
cd /usr/local/binsince it is the only matching pattern
There are a lot more features than what we've shown here, but at least this gives you an idea as to how shells can be different.
The Bash shell (also known as the "Bourne-again shell") was also released around the same period as the Z shell (in 1989) and Brian Fox is regarded as the creator behind it. It was initially written as a replacement for the Bourne shell. For many years it has shipped as the default shell for GNU, most Linux distributions, and Mac OS X (version 10.3+). Like a true predecessor should, Bash is capable of executing all of the Bourne shell commands without a problem.
There are quite a few features that the Bash shell has and some of the lesser-known ones include:
- Insert the last parameter(s) of the preceding command in your current command using
- You can keep a process running even after logging out. To do so, use the command
disown -h <pid>where you will have to place the process ID (PID) of the program instead of
- Execute the previous command again, but this time running with
sudousing the command
!!is shorthand for 'the previous command')
- Perform a reverse incremental search using the
- Press tab twice and you will see the list of completions for the word that you have just typed or are typing
- When executing a script with
bash, use the
-xoption to output the script contents as it's being executed
If you want to learn more, you can see a much larger list of Bash-specific features here.
Comparing Z shell and Bash
Now that we've give you a brief introduction to both of the shells, let's see how they hold up when actually compared and contrasted together.
The first thing to look at (and one of the more significant aspects, in my opinion) is prevalence and popularity of the shell. While the Z shell has its fair share of users throughout the developer community, it's usually safer to write your scripts for Bash since there is a much larger group of people that will be able to run those scripts.
The importance of adoption holds true for the public resources and documentation as well. Thanks to its large community, Bash has quite a few more resources out there to help you learn how to use it.
So, if you are planning on writing a script that you want many developers to easily be able to run then I'd recommend that you go with Bash. However, this shouldn't stop you from using Z shell if your end goal is more suited to Z shell. Finding the right solution to a problem is much more important than just using what's popular, so keep that in mind as well.
Although Bash is much more popular, that doesn't mean Z shell is without any useful features of its own. It's actually heavily praised for its interactive use, because it's more customizable than Bash. For example, the prompts are more flexible. It can display a prompt on the left and another on the right side of the screen, much like vim's split screen. The auto-completion is also more customizable and is actually faster than Bash's.
To give you a better sense of what kind of features Z shell has, here is a list of things that you will have access to when using Z shell over Bash:
- The built-in
zmvcommand can help you do massive file/directory renames. e.g. to append '.txt' to every file name run
zmv –C '(*)(#q.)' '$1.txt'
zcalcutility is a great command-line calculator which is a convenient way to do a quick calculation without leaving the terminal. Load it with
autoload -Uz zcalcand run with
zparseoptscommand is a one-liner that lets you to parse complex options that are provided to your script
autopushdcommand helps you do
popdafter you use
cdto go back to your previous directory
- Floating point support (which Bash suprisingly does not have)
- Support for hash data structures
There are also a bunch of features that are present in the Bash terminal but are absent from almost all of the other shells. Here are a few of them as well:
–norccommand-line option, which allows the user to proceed with the shell initialization without reading the
- Using the option
bashallows you to execute commands from the specified file
- Excellent invocation features
- Can be invoked with the
- Bash can be run in a specific POSIX mode. Use
set –o posixto invoke the mode or
- You can control the look of the prompt in Bash. Setting the
PROMPT_COMMANDvariable to one or more of the special characters will customize it for you
- Bash can also be invoked as a restricted shell (with
--restricted), which means certain commands/actions are no longer allowed, such as:
- Setting or unsetting the values of the
- Redirecting output using the '>', '>|', '<>', '>&', '&>', and '>>' redirection operators
- Parsing the value of SHELLOPTS from the shell environment at startup
- Using the exec builtin to replace the shell with another command.
- And many more...
- Setting or unsetting the values of the
It's difficult to say which shell is actually better. It all really depends on your own preferences and what you actually want to do with the shell. In the case of Bash vs Z shell, neither is really better than the other.
There are quite a few fans of the Z shell throughout the developer community who advocate for it quite heavily thanks to the many useful features it provides. On the other side, there are even more fans of Bash who know that their biggest advantage is their far larger user base. It's easy to see why it is so difficult to get developers to switch from Z shell to Bash and vice versa.
So where do you go from here? There are a lot of details to master when learning to write shell scripts. If you want to learn more, I'd suggest taking a course like the BASH Programming Course: Master the Linux Command Line, which will teach you just about everything you need to know about scripting (specifically in Bash), starting with beginner concepts and working its way up to advanced topics.
Hopefully now you have an idea as to how both Bash and Z shell differ, as well as their similarities. Quite a few resources exist out there for both shells, so you won't have any trouble finding more information if you need it.
Which shell do you prefer, and why? Let us know in the comments section!