Is it recommended to use zsh instead of bash scripts?

For portability, no. While zsh can be compiled on any Unix or Unix-like and even Windows at least via Cygwin, and is packaged for most Open Source Unix-likes and several commercial ones, it is generally not included in the default install.

bash on the other end is installed on GNU systems (as bash is the shell of the GNU project) like the great majority of non-embedded Linux based systems and sometimes on non-GNU systems like Apple OS/X. In the commercial Unix side, the Korn shell (the AT&T variant, though more the ksh88 one) is the norm and both bash and zsh are in optional packages. On the BSDs, the preferred interactive shell is often tcsh while sh is based on either the Almquist shell or pdksh and bash or zsh need to be installed as optional packages as well.

zsh is installed by default on Apple OS/X. It even used to be the /bin/sh there. It can be found by default in a few Linux distributions like SysRescCD, Grml, Gobolinux and probably others, but I don't think any of the major ones.

Like for bash, there's the question of the installed version and as a consequence the features available. For instance, it's not uncommon to find systems with bash3 or zsh3. Also, there's no guarantee that the script that you write now for zsh5 will work with zsh6 though like for bash they do try to maintain backward compatibility.

For scripts, my view is: use the POSIX shell syntax as all Unices have at least one shell called sh (not necessarily in /bin) that is able to interpret that syntax. Then you don't have to worry so much about portability. And if that syntax is not enough for your need, then probably you need more than a shell.

Then, your options are:

  • Perl which is ubiquitous (though again you may have to limit yourself to the feature set of old versions, and can't make assumptions on the Perl modules installed by default)
  • Specify the interpreter and its version (python 2.6 or above, zsh 4 or above, bash 4.2 or above...), as a dependency for your script, either by building a package for every targeted system which specifies the dependency or by stipulating it in a README file shipped alongside your script or embedded as comments at the top of your script, or by adding a few lines in Bourne syntax at the beginning of your script that checks for the availability of the requested interpreter and bails out with an explicit error when it's not, like this script needs zsh 4.0 or above.
  • Ship the interpreter alongside your script (beware of licensing implications) which means you also need one package for every targeted OS. Some interpreters make it easier by providing a way to pack the script and its interpreter in a single executable.
  • Write it in a compiled language. Again, one package per targeted system.

No, you cannot. What is guaranteed available is /bin/sh, essentially the original Bourne shell. Almost all (note the "almost"!) Linux installations will have bash, but on *BSD systems it is rare (the BSD persuation has severe misgivings about GPL code, like bash). I don't know what the standard shell is on Mac, but again, there are misgivings about GPL. Same for Solaris.

zsh is a niche shell, it isn't in the default install of Fedora (and I don't believe it is default for any major distributon).


I think it really depends on what zsh extra features you're using and where. If you're planning to distribute your script across the different users and systems I would suggest to use bash or even sh.

The same time if your script designed to be executed across your organization and you have convention to have zsh on your machines (for instance the same basic AMI) and your task have clear benefits from extensions like advanced file selectors or other stuff from http://www.rayninfo.co.uk/tips/zshtips.html I would say go ahead!