Armand Halbert
Customer Success Engineer

Resume - pdf
Resume - text

Published on:
Thursday, November 19, 2015

From Bash to Zsh

Conquering the Command Line

This is not the first time I had tried to switch to zsh, but after running into problems with bash that could have been fixed by using zsh, I decided to give it another go. I ended up picking this book since it compared bash and zsh, offering an easy zsh upgrade path by showing you how to duplicate your bash configuration in baby steps.

I highly recommend From Bash to Zsh for any intermediate shell user, it contains a wealth information about how to use the shell more productively. And for the advanced bash users, it shows how to accomplish familiar tasks in bash, and then shows how zsh improves on bash features. This makes It gives a helpful enough overview for users to discover zsh on their own, but does not bog down the details, except in matters that could break the shell.

However, it assumes you have used the shell often before; very few of the commands you use on the shell are discussed, even important shell tricks such as xargs. Sadly, I cannot recommend this book to a unix beginner.

But I do recommend they use zsh if possible.

###Why ZSH?

I didn’t think bash really needed to be replaced; after all it came with every *nix system, where zsh often has to be installed, and that portability was king for shell scripts. And, after all how much more productive would zsh make me? Zsh, however does not have to be your script shell, and zsh proves how much better a shell could be. At the same time, it’s similar enough to the other shells that not being able to use zsh won’t screw up your workflow too much.

####Compinit compinit is the zsh completion system. It is far smarter and customizable than bash-completion, even after installing extensions for bash-completion.

Unlike bash, zsh comes with completions for common unix utilities, and many ship their own zsh completion files. compinit also distinguishes between files, builtins, commands, and environment variables.

compinit with argument help

Zsh can note if a completion is a file, command, script or builtin.

zsh menucompletion

It can even provide helpful information about commands. For someone learning, compinit is invaluable and easier than scouring man pages for an obscure argument.

####Better globbing

* is one of the most powerful features of bash, to the point where it’s friends ? is completely forgotten. Zsh introduces a far more powerful, allowing you to glob files by properties such as file size and creation date. Most importantly, you can glob entire directory tree with all of these completions, avoiding hacks with programs like xargs.

###Pitfalls of switching

Of course, the first time I switched to zsh I failed. Why did zsh work for me this time?

####zsh is not bash++

Hearing that zsh was designed to be bash-compatible, I foolishly copied my .bash_profile into .zshrc expecting it to work, and was dissapointed when things like menu-completion did not work as expected.

Bash was developed in 1989, and zsh in 1990. Bash at the time was mostly developed as a GNU alternative to the Bourne shell, and Zsh was trying to be a more useful sh. While they try to be compatible, they are fundamentally different projects with a long history.

####Don’t use OMZ (Yet!)

There’s a lot of love for the oh-my-zsh project, so I was surprised when all #zsh questions with OMZ were directed to #oh-my-zsh, as if it were a completely separate project, and in general, did not approve of the project. This was because OMZ changes a lot of zsh behavior. I for example, found my completions did not work after I installed zsh. OMZ was a reason I found zsh so frustrating, as it changed settings without me understanding those settings existed. I first rebuilt my bash experience, then converted to OMZ.

That’s not to say OMZ is bad or useless. After all, does offer a lot, from mac-specific features, to themed prompts. But those features are icing on the cake; After all, Do you really need such a glorious prompt? zsh extravagance

OMZ and friends can make your zsh much better. But understand zsh before you move to OMZ.

Note that the advanced completions are not exclusive to OMZ, and are stored at github.

####Write most scripts to be sh-compatible

Zsh has a lot of nice features for scripting compared to sh. One example is word splitting, splitting predictably on input, rather than how sh splits on whitespace in confusing ways. Zsh’s interpreter makes shell script almost as good as Python and Perl, though still distinctly a flavor of sh.

However, the reason you’re using shell scripts instead of a higher language in the first place is portability, and zsh is not sh (even having a special sh-compatible mode). It’s not POSIX compatible insanity, but it’s still inherits the extremely clunky sh language.

###The Verdict: Z Shell
Compinit alone is worth the switch, even if you never use a single other zsh feature. However, to the more advanced zsh user, the shell becomes a thing you want to use, rather than a thing you have to use.

<< Previous Next >>