Why you can trust Sunlight Media
- Expertise and Experience:Our content is crafted by seasoned professionals with extensive experience in digital marketing, ensuring you receive accurate and actionable advice.
- Unbiased Information:We provide impartial insights and recommendations based solely on what's best for your business, without any hidden agendas or promotions.
- Thorough Research:Our articles are backed by comprehensive research and the latest industry trends, ensuring you stay informed with reliable and up-to-date information.
- Transparency and Honesty:We believe in complete transparency. We disclose our sources, methodologies, and any potential conflicts of interest, so you can trust the integrity of our content.
- Continuous Improvement:We constantly review and update our content to reflect the latest developments in digital marketing, so you always have access to the most current and relevant information.
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.
The z-command
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.
Auto-completion
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.
Auto-correction
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.
Color customization
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.
Installing zsh
If you are on OS X, you already have zsh installed on your system. To check, run the following command:
which zsh
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.
Configuring zsh
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.
Git branches
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.
Conclusion
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.
38 Comments
“bash (an abbreviation for “Bourne-again shell”) is the default shell for Unix-based operating systems…”
This is not true. Bash is the default interactive shell on most Linux distributions and macOS, yes. It is, however, not the default – or even included in the base system – on any BSD variant I’ve come across. Commerical Unix systems, e.g. AIX, don’t use Bash either.
Also, Linux is not Unix-based (based on original Unix code), but Unix-like (designed after the Unix way of doing things).
Thanks for pointing that out. The post has been updated to reflect this.
A lot of the benefits of zsh mentioned here are also available for bash via bash-it https://github.com/Bash-it/bash-it
This is true, although as the README.md notes, it’s a “shameless ripoff of oh-my-zsh” 🙂
Note, Bash also has some of the features listed above (when configured):
– The ‘z’ utility is shell agnostic, it works on Bash and Zsh. I use it all the time in Bash. See here https://github.com/rupa/z/
– With bash-completion installed and inputrc configured appropriately it is possible to setup Bash to have Zsh AUTO_MENU like completion (including option cycling).
– The `cdspell` and `dirspell` options will do auto filesystem name correction in Bash
– Directories and files can be configured in different colors via LS_COLORS variable in Bash
– Command-click URLs are a terminal capability. In iTerm2 I can do COMMAND+CLICK on a URL and launch a browser.
I discuss many of those Bash capabilities and settings here: https://bluz71.github.io/2018/03/15/bash-shell-tweaks-tips.html
Bash can do 90% of what Zsh can do, it just requires some tweaks.
Zsh does have some nice capabilities that Bash does not have, those being:
– Right-hand side prompt (if you want it)
– Command syntax highlighting
– Descriptive tab completions (man page derived)
Cheers.
“…unless you type pwd”, not quite true. Take a quick look through the man page for bash/ksh and search (/ like vi) for PROMPTING. Here you’ll find how to set up your prompts (PS1 is the primary and there are others for different uses PS2..4). Specifically to put he info you’d like – /w gives the path.
However, the scope for customization it is quite limited when compared to OMZ.
That click and open thing is a function of your terminal emulator, not zsh. Also option completion as well as the z command are plugins for zsh, which are in similar form available for bash too. colour higlighting of files is built into bash as well.
Why am I able to edit this blog?
thanks for the post, i love `z` command very much, didn’t know of this plugin!
btw, you’re setting contenteditable=”true” to your div(s), but not the titles like `h2`
How are you able to edit the blog?
thanks for the feedback Phat Tran.
“With the release of Version 7 Unix in 1979, the new OS came distributed with bash as its default shell.”
This statement is not correct.
The default shell in Version 7 (and subsequent UNIX-branded operating systems) is the Bourne Shell.
bash was created in 1989, by Brian Fox, as part of the GNU project. (https://en.wikipedia.org/wiki/Bash_(Unix_shell))
Thank you. I was reading the article and saying to myself: “This is false/incorrect/irrelevant”. I would like the author to update the post according to your comment. Otherwise it simply shows that they don’t know much about shells…
“…unless you type pwd”, not true at all! Most Linux distros and (I’m pretty sure) macOS all come with PS1 set to something useful that includes the cwd. Typing `pwd` every time you need to see where you are is not an effective workflow and next to nobody does that.
This article is more a comparison of the default configuration in bash and zsh rather than the features of the shells themselves.
Okay, tried it, it barfed on every line of my .bash_profile. Not so very compatible I guess.
Also I’m pretty sure /bin/sh was just as scriptable since I used it for 15 years. Well, that an ksh.
Nice guide! This entry is about to blow up due to Apple’s announcement of the switch to ZSH in Catalina. 🙂
What’s the theme used in the examples?
This is a nice article. However, there are only a couple of zsh features listed here that are truly unavailable in bash (unless you are using macOS’s ancient version of bash).
For example, tab auto-completion works in bash. You do not, as the article states, know the full pathway for a cd command. All you have to know are partial directory and file names if you are willing to tab through the levels you don’t know exactly.
It’s also misleading to say that bash won’t show the current working directory in the prompt (by default, neither does zsh, but both are easily amended to display that information) or that bash doesn’t play well with git, as there are packages for that.
I’m not saying these things are equivalent to their zsh counterparts. They aren’t, and many people like the zsh variants better. What I am saying is that I found the article less compelling because the bash side of the conversation felt like a straw man. Maybe you could have run this past a fan of bash before printing it so that they could have helped you root out the misleading bits.
On the other hand, there are some things zsh does that are truly not part of bash, that you did not even touch on, like recursive path expansion that allows you to type “cd /u/sh/cal” and have it auto-expand into “cd /usr/share/calendar.”
And the standout feature of zsh seems to be its builtin level of customizability. For example, if you run something like “set -o | grep ‘on|off’ | wc”, you will find that there are 179 shell options exposed in zsh compared to only 27 in bash.
Like I said, good stuff. Next time, maybe run your stuff past a bash enthusiast before saying what bash can’t do.
Something I find very useful, is to set stderr to be red, and stdout black. There are of course ways to do this in bash (and I assume in zsh) for a single command, but doing it for a session seems to be harder. I have a perl script that starts at startup that does this , but that seems like using a sledge hammer to kill a fly. There are some other solutions here: https://unix.stackexchange.com/questions/12439/can-i-configure-my-shell-to-print-stderr-and-stdout-in-different-colors. But I’m surprised this doesn’t come standard with some shell or other (I came here hoping it came with zsh…).
The legacy Unix shell was written by Steve Bourne, not Ken Thompson – hence bash being called the ‘Bourne-again’ shell.
Your short history of bash is incorrect. The shell that replaced the Thompson shell in 1979 is the Bourne Shell (sh), not BASH. The original Bourne Shell (sh) is not the same as BASH. BASH did not arrive until 1989 with the GNU Project. (https://en.wikipedia.org/wiki/Bourne_shell)
except for the “z filepath” command, everything else is available in bash.
wake me up when there’s a real replacement for bash.
Several mistakes in your history lesson.
Bash was certainly not released in 1977. As others have noted, you’ve confused Bash and the Bourne shell (not the same thing). It was Bourne shell that was released with Research UNIX. Bash was never mentioned in The UNIX Programming Environment; that book was published 5 years before Bash was even coded.
Note about security:
What is your assertion that Zsh offers better security based upon? As you mentioned Bash had an undiscovered vulnerability for years, but what makes you think Zsh doesn’t have undiscovered security vulnerabilities? I don’t think you’ve offered any valid reasons why Zsh would be more secure than Bash.
Bash definitely was not released with Version 7 Unix. Bash is a GNU project and was first released in 1989.
What is the color theme used for powerlevel9k?
Thank you for your constructive feedback on this post. The post has been updated to reflect your comment.
Thank you for pointing out this error, Trix Farrar. The post has been updated to reflect your comment.
Thank you Chris for your helpful insight on bash and sharing your concerns. The post has been updated to present bash and zsh more accurately based on your comment.
Thank you for pointing out this error, Peter Birch. The post has been updated to reflect your comment.
Thank you for pointing out this error and sharing your concerns with the post, Robert Eksten. The post has been updated to reflect your comment.
Thank you for pointing out this error, Kate Ebneter. The post has been updated to reflect your comment.
Thank you for the positive feedback, Pete! The post has recently been updated to also reflect this new announcement regarding macOS Catalina.
Perhaps this article can provide some help?
https://github.com/Powerlevel9k/powerlevel9k/wiki/Show-Off-Your-Config
Haha just having a good time reading the comments… As all unix tools around for nearly 40 years, each one is a lifestyle rather than a simple tool. I am sure you can even port DOOM to bash or ZSH or SendMail or Vi.
Yes, that’s a good point Robert. Thanks for sharing your thoughts.
At least they’re not making tcsh the default.
Apple had better not make bash unavailable.
One major difference between bash and zsh has not been mentioned here. It’s the way the command history is being kept.
When you have multiple bash’s open only commands typed into the first opened are recorded to the history file (.bash_history), which is only written to disk once the first shell has been terminated. (Please correct me if I’m wrong here.)
So in other words, commands typed into a second bash will not be recorded.
The zsh records all commands on all opened zsh’s to the history file (.zsh_history) together with a time stamp.
That’s proper accounting! 😉
That and the “fancy colours” (I’m using oh_my_zsh :-D) were the reasons why I converted.
Thanks for the feedback!