Zsh (short for Z-Shell) is, in technical terms, a UNIX Command Interpreter (often nicknamed shell by the community) , and in more simple terms a command prompt for UNIX and Linux based computer systems.
Yesterday, Software Engineer Ali Spittel announced on Twitter that Apple's macOS Operating System will be changing its default shell to Zsh from Bash.
I have been a long-term Zsh user, and I learned about Zsh from Thoughtbot's laptop-setup-script on GitHub about a year or two ago. Since Zsh will become "the new normal" for macOS users, I decided to write about what it is and why you should be using it over Bash.
What advantages does Zsh bring over Bash?
Zsh borrows a lot of features and functionality from both Ksh and Csh and brings in the best of both while still adding its own spin on things. A few special features I liked that appear to be unique to Zsh (some of these may require you to enable them in autoload first) are as followed:
- zmv, a built in tool to do mass renames of files using a pattern. For example, to rename every file in a directory to an HTML file you could run
zmv –C '(*)(#q.)' '$1.html'.
- zcalc, a built in calculator to zsh, it takes the same syntax as most programming languages to evaluate and return the result of a mathematical expression.
- Expanded command syntax to allow advanced globbing and recursive searches.
- Redirects, you can type
echo "Hello World" >log.txt >&1to print "Hello World" to log.txt and to the stdout at the same time. This makes permanently logging output as you see it possible.
Those are cool but that's just scratching the surface, the following sections look at the bigger parts of Zsh.
A powerful plugin ecosystem
Another feature of Zsh that I am a fan of is the powerful plugin ecosystem that comes along with it. In the screenshot at the beginning of this post, I am using a terminal emulator called Hyper Terminal to run Zsh which shows the Git plugin in action as it displays a project's branch information.
You can see a nice list of plugins on GitHub with the awesome-zsh-plugins list. If you want a suggestion of something to try out as you learn how to use zsh plugins, and you're a frequent reader of my blog, please consider the crystal-zsh plugin. It adds a lot of useful aliases for Crystal Developers like me and saves on typing time.
Several community maintained frameworks
Getting started with Zsh can be complicated, so can all of the necessary installs of plugins and themes. The community has recognized this and created several frameworks to help you get started faster with Zsh. For a beginner I recommend oh-my-zsh, it's easy to get started with and has quite the community behind it. It's very well documented and was easy to setup and configure.
Sweet, sweet, shell themes
Zsh supports themes, just like you can install a custom theme in your text editor, Zsh itself has themes to make your shell look as appealing as possible while you work with it. I recommend taking a look at oh-my-zsh's themes page on their Wiki if you want to get started with choosing a theme. You can even have Zsh pick a theme at random (out of your installed themes) each time you open a new session.
No long-lived command history
Unlike Bash which is built-into the current version of macOS, Zsh does not have a long-lived command history (I'm not a fan of command histories because of privacy concerns and when I used Bash, I had to clear mine frequently and it was annoying to say the least). While it's still possible that Apple will make changes to their builds of Zsh to enable the command history by default (which has a setting to disable it), a reason I'm a fan of Zsh is that the command history is turned off by default.
The build of Zsh that I run is from the Homebrew package manager and it lacks the command history (at least by default) and that has built some trust between Zsh and I. The builds on Homebrew do not include Apple's changes to Zsh so the history concern isn't an issue for me yet. In the future it could be but not today.
It's worth noting here that when installing the framework oh-my-zsh, it's worth noting a command history will be enabled. Remember to adjust your zsh configuration accordingly if this is an issue for you.
This is a smaller behavior of Zsh, and while some people may dislike it, the feature aligns with my personal values and is yet another reason I choose to support Zsh. The behavior shows Zsh respects user choice and takes an opt-in approach before collecting any data about the user's activity (even locally).
Who maintains it and develops new features?
There is a Zsh mailing list where those who wish to can discuss changes to the software and contribute patches. The community development of Zsh is one of the things I like most about it. There's no right or wrong way to submit a patch, although if you submit to the mailing list you'll probably receive some criticism and people are free to choose whether or not to include your changes in their builds of Zsh.
Where can I learn more before I install Zsh?
I would recommend reading over awesome-zsh (not to be confused with awesome-zsh-plugins) on GitHub, it provides even more information on why Zsh is awesome. If you have a more specific question about Zsh, search engines are your friend. Ali's article was also a good read and showed some cool things you can do.
This post required a lot of research, reading, and experimenting to learn about Zsh's features and unique tools (a few I didn't know about until the time of writing). I don't hate Bash and I still use it on quite a few production servers of mine, but Zsh continues to impress me for my local development environment. I hope I've convinced you to give Zsh a try.