Hacker News new | ask | show | jobs
by aeadio 838 days ago
Zsh is quite a bit nicer than Bash for scripting. For one, parameter expansion doesn't word-split by default, so you don't have to haphazardly quote everything. There's also some really powerful features, like the parameter expansion flags [1] and filename generation (globbing) flags [2] that can be combined to do very flexible things. [3]

Zsh also bundles a ton of useful modules that can be used to do things pretty outside the box. Eg, there's a module for creating and controlling a hidden PTY. [4]

Zsh also has some quite nice shorthands for basic grammar, like compact loops and conditionals. Eg, you can do:

    for foo in bar*;
      do_stuff # you don't need the do..done here for single-line for-loops
Also, while Zsh diverges from POSIX sh in some areas (like word splitting), Zsh has sh- and Bash-compatible emulation modes, which can even be mixed together within single scripts (eg functions can set their emulation mode and shell options locally, or files can be sourced or commands run with different emulation modes prefixed). [5]

Lastly, Zsh has this weird reputation for being slow, because the configuration frameworks like oh-my-zsh are absolute dogs. But it's actually among the faster shells, with very respectable script performance that's squarely ahead of Bash. [6] Interactive performance can also be fantastic if you use a less hungry framework or write your own config, and Zsh has many tools to improve speed further, like support for pre-compiling source files [7] and pretty good support for async workers. [8]

Zsh has also been around about as long as Bash, and runs on every major platform and is available in just about every platform's package manager. So the cliche about Bash being available everywhere is just as true for Zsh. Zsh is honestly a much better replacement for scripting, but Bash benefits from already established traction and by being the default shell on most Linux distros.

[1] https://zsh.sourceforge.io/Doc/Release/Expansion.html#Parame...

[2] https://zsh.sourceforge.io/Doc/Release/Expansion.html#Globbi...

[3] https://thevaluable.dev/zsh-expansion-guide-example/

[4] https://zsh.sourceforge.io/Doc/Release/Zsh-Modules.html#Zsh-...

[5] https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Command... (see 'emulate' command)

[6] https://towardsdatascience.com/comparing-sh-bash-ksh-and-zsh...

[7] https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Command... (see 'zcompile' command)

[8] https://github.com/mafredri/zsh-async as an example, but also doable in bare Zsh