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?

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
Jason Baker
  • 1,229
  • 6
  • 20
  • 25
  • 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. – Charles D Pantoga Jun 16 '17 at 20:01

8 Answers8

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.

David Mackintosh
  • 14,223
  • 6
  • 46
  • 77
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.

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
  • 3
    That would be every GNU/Linux system you mean? – andol Dec 11 '09 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? – Kyle Brandt Dec 11 '09 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 '09 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 '09 at 15:01
  • andol: Those are the ones I used to use.. I must remember incorrectly, or I did that and just don't remember. – Kyle Brandt Dec 11 '09 at 15:03
  • 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 '16 at 19:57
13

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.

Phil P
  • 3,040
  • 1
  • 15
  • 19
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).

Roboprog
  • 191
  • 4
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"

cavalcade
  • 311
  • 1
  • 3
  • 6
  • 3
    How is bash copying features from zsh a reason to use bash? – qqx Oct 27 '12 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 '20 at 01: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.

Craig
  • 51
  • 1
  • 1
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.

Samat
  • 149
  • 2
  • 2
    Many modern implementations of sh are just links to bash. – user9517 Aug 21 '12 at 11:39
  • 2
    @Iain Ubuntu uses [dash](http://en.wikipedia.org/wiki/Debian_Almquist_shell) by default, and even if `sh` is bash, it enables compatibility mode when run as `sh`. – mgorven Aug 25 '12 at 03: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. :).

  • 3
    I think that works with ZSH as well, with tab completion in ZSH, I get lots of program specific arguments ... – Kyle Brandt Dec 11 '09 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? – Wojciech Kaczmarek Dec 11 '09 at 15:13
  • 1
    @Wojciech Kaczmarek: I think you want the 19.3 from the following: http://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.... – Kyle Brandt Dec 14 '09 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 02 '10 at 03:06