36

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?

1
  • 3
    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. Jun 16, 2017 at 20:01

8 Answers 8

33

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.

1
  • 2
    I recall having ksh88/ksh93 on HPUX, we never had access to install bash.
    – Zlemini
    Feb 3, 2017 at 15:25
25

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.

6
  • 3
    That would be every GNU/Linux system you mean?
    – andol
    Dec 11, 2009 at 14:19
  • 3
    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? Dec 11, 2009 at 14:27
  • 2
    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.
    – user5336
    Dec 11, 2009 at 14:41
  • 3
    Must been different BSD:s then. At least in OpenBSD and in FreeBSD you have to install bash separately from ports/packages.
    – andol
    Dec 11, 2009 at 15:01
  • 1
    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.
    – Kevin
    Sep 27, 2016 at 19:57
14

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.

1
  • 7
    nb: ${foo:offset:len} is now supported in zsh, for compatibility.
    – Phil P
    Aug 30, 2011 at 9:18
9

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).

5

In addition to the portability reason given above, another one might be that bash is still adding features.

For example bash v4.x+ introduced:

  • recursive globbing:

    rm -f **/*.log

  • autocd:

    Type "/tmp" instead of "cd /tmp"

2
  • 3
    How is bash copying features from zsh a reason to use bash?
    – qqx
    Oct 27, 2012 at 20:35
  • 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?
    – Oliver
    Apr 22, 2020 at 1:33
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.

1
  • 4
    Can you explain which features or ideas in zsh you find to be half-baked and why?
    – funroll
    Jun 6, 2015 at 4:11
3

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.

The right way to write portable unix scripts is to use sh, which is found in every *nix system.

Every other shell except sh is just a daily interactive tool of yours.

When coming to bash vs zshbash is bundled in more *nixes than zsh, so here comes disadvantages of installing additional software to the system — you have to maintain it. Some folks love zsh "cool features" so they like to pay that price, some not.

2
  • 3
    Many modern implementations of sh are just links to bash.
    – user9517
    Aug 21, 2012 at 11:39
  • 2
    @Iain Ubuntu uses dash by default, and even if sh is bash, it enables compatibility mode when run as sh.
    – mgorven
    Aug 25, 2012 at 3:39
2

Another point:

Many programs provide cool bash completion by default. For me it's the reason to not switch.

[added in Jul 2013] Well after few years of using zsh since the comment above, I have to say tab completion (even builtin one, w/ no 3rd-parety modifications) is brilliant and looks like it's far beyond the one offered by bash. :).

4
  • 3
    I think that works with ZSH as well, with tab completion in ZSH, I get lots of program specific arguments ... Dec 11, 2009 at 14:43
  • 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? Dec 11, 2009 at 15:13
  • 1
    @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.... Dec 14, 2009 at 12:41
  • 5
    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
    Jan 2, 2010 at 3:06

Not the answer you're looking for? Browse other questions tagged .