Is there any reason to use bash over zsh?

22,796

Solution 1

Two reasons come to mind:

First -- it is available practically everywhere. I have several Linux systems (CentOS 4.x in this case) which do not have zsh installed. Similarly I have to touch ancient systems like Solaris 2.6 and up, HP-UX 10 and up, and similarly creaky versions of AIX. Therefore I pretty much have to use bash on these computers, which I do because I touch dozens, if not hundreds, of individual computers over the course of a month, and in order to get consistency in your interface you end up being stuck using the defaults.

Second -- it is available practically everywhere. This means I can write a bash shell script and be 99% sure that it will work when transferred elsewhere.

Yes, these reasons are superficially the same, but the reasoning behind them is different.

Solution 2

Bash generally comes with every system, zsh doesn't. I love zsh, but because of this, I use zsh for interactive use, but Bash for all my scripting.

I find this keeps everything simpler, as even when I shopt whatever the bash compatible (setopt SH_WORD_SPLIT ?), I still run into subtle differences.

Solution 3

zsh is not fully bash compatible. There are a variety of differences. Newer zsh is more compatible with bash (=~ supported, exec now has the extra flag options, etc) but full compatibility is not a goal, not even under "emulate".

For instance, bash substring is ${foo:offset:len} but in zsh it's $foo[start,end] and that's just one simple example.

zsh is a tcsh and ksh influenced shell which does many things its own way; POSIX compatibility is explicitly not a goal, but the developers are responsive to patches which add options/emulate behaviour that get things closer to POSIX. But when you start really getting into the power of the shell, you start creating write-only scripts, more so even than bash.

bash is POSIX sh + ksh + pedanticism, with some features now copied from zsh. It too has write-only scripts but because it has less powerful operators, you end up not using the conciseness of zsh and things might be more readable (except for all the quoting to avoid whitespace split, the stupid ksh-style $array means first-element-of-array, not all-elements-of-array, etc etc).

Writing scripts which take full advantage of the power of either shell is unwise, unless you're in a constrained environment (eg, writing system rc scripts, where some FSs might not be mounted, etc). As an ideal, use Perl/Python/Ruby/whatever for anything big enough that you need the expressiveness not in Bourne sh, if you want others to be able to maintain it. Keep the shell stuff for things relating to the interactive shell (tab completion programming, etc).

I wouldn't use bash over zsh. I'd use bare sh over zsh for simple scripts, or switch to a language where associative arrays have decent operators (unlike in zsh, where they are, again, 'concise'). I might switch a sh script to bash if I need that one little feature to extend an existing proven-working script and don't have time to rewrite it now.

Solution 4

My advice: if you are going for absolute portability, write using Bourne shell rules, don't even bother with Korn shell extensions. As mentioned, that are some older "big boxes" around that don't have GNU shells on them at all.

Bash already does "too much". I have a friend at work who prefers zsh, but I don't know what exactly it does.

Anyway, either write for Bourne (or "bourne again") shell, or alternately, if you are doing a custom script for a small number of specific boxes, skip "shell hell" entirely, and just write using perl or python (or whatever your favorite locally installed interpreter is).

Solution 5

By the way, you have been told here many times that bash is found practically everywhere, so use it to write portable scripts, which is false.

Nonsense. If you know that every system you care about has BASH then it's a perfectly reasonable statement. BASH has many useful features that can't be reasonably emulated in POSIX sh. Frivolous use of non-POSIX features is not a great idea but using them when you genuinely need them is fine.

Portability is not an absolute. It's arguable how far you need to take it, just like anything else. For example, Fedora uses shell commands for building RPM packages and the Fedora packaging guidelines state that since all packages must be built natively on Fedora, it's fine to use all the features of BASH. Even though theoretically other distros without BASH may want to reuse their source RPMs, they made a decision about portability based on practicality instead of your "ALWAYS UZE POSIX!!1" mantra.

ZSH hasn't caught on as a default shell, simply because it's a sprawling mess of half-baked ideas.

Share:
22,796

Related videos on Youtube

Jason Baker
Author by

Jason Baker

I'm a developer on Google's Cloud Console.

Updated on September 17, 2022

Comments

  • Jason Baker
    Jason Baker over 1 year

    I'm curious about why one would want to run bash instead of zsh. I mean zsh is fully backwards-compatible with bash. Don't get me wrong: I don't dislike bash or anything. I just genuinely want to know if there are any advantages to using it over zsh.

    So what reason is there to use bash over zsh?

    • Charles D Pantoga
      Charles D Pantoga almost 7 years
      Just wanted to point out that ZSH is not fully backwards compatible with BASH. In ZSH, array indexing starts at 1 -- in BASH, array indexing starts at 0. There are other differences as well, but I wanted to point this one out.
  • elmo
    elmo over 14 years
    That would be every GNU/Linux system you mean?
  • Kyle Brandt
    Kyle Brandt over 14 years
    Well I have found it comes with OS/X and bsd, I don't have much experience with other *nix's , do they (recent versions) not usually have bash?
  • user5336
    user5336 over 14 years
    Older Solaris versions didn't include bash; I typically had to depend on Bourne being the shell for scripting. This hasn't been the case for a while, but you may still run into it in some big enterprises.
  • Kyle Brandt
    Kyle Brandt over 14 years
    I think that works with ZSH as well, with tab completion in ZSH, I get lots of program specific arguments ...
  • elmo
    elmo over 14 years
    Must been different BSD:s then. At least in OpenBSD and in FreeBSD you have to install bash separately from ports/packages.
  • Kyle Brandt
    Kyle Brandt over 14 years
    andol: Those are the ones I used to use.. I must remember incorrectly, or I did that and just don't remember.
  • Wojciech Kaczmarek
    Wojciech Kaczmarek over 14 years
    Interesting. Are you saying that bash completion files work with zsh, or just some programs have its zsh-specific completion stuff? Still there's some std layout of bash completion files which zsh may or may not consider automagically. Have you tweaked your zsh config to make it work?
  • Kyle Brandt
    Kyle Brandt over 14 years
    @Wojciech Kaczmarek: I think you want the 19.3 from the following: zsh.sourceforge.net/Doc/Release/zsh_19.html . Honestly, I never took the time to really understand it, just ripped off a .zshrc I found online :-) But it will complete command line arguments from programs like apt, grep, etc....
  • Phil P
    Phil P over 14 years
    bash's tab completion is very similar to zsh's old tab-completion system. This is one of the ideas copied from zsh to bash (there are others copied bash to zsh). You can use completion scripts written for bash in zsh, by running "bashcompinit" and then using the "complete" and/or "compgen" commands inside zsh -- this is possible because the bash functionality is a subset of that available in zsh and so can be emulated. So there's no reason to switch to bash because a command ships with a bash completion.
  • Phil P
    Phil P over 12 years
    nb: ${foo:offset:len} is now supported in zsh, for compatibility.
  • user9517
    user9517 over 11 years
    Many modern implementations of sh are just links to bash.
  • mgorven
    mgorven over 11 years
    @Iain Ubuntu uses dash by default, and even if sh is bash, it enables compatibility mode when run as sh.
  • qqx
    qqx over 11 years
    How is bash copying features from zsh a reason to use bash?
  • funroll
    funroll almost 9 years
    Can you explain which features or ideas in zsh you find to be half-baked and why?
  • Kevin
    Kevin over 7 years
    For compatibility, you really shouldn't use bash, either. It's pretty much everywhere, but even more systems provide a POSIX-compliant /bin/sh (which may or may not actually be bash). Unless you're doing something complicated where bash's extensions would be particularly helpful, I wouldn't use bash specifically.
  • Oliver
    Oliver about 4 years
    On my system I have to activate autocd with shopt -s autocd and recursive globbing with shopt -s globstar. That being said, are you implying that zsh is not adding features?