Does Bash have a way to access arrays similar to Zsh, something like

$ foo=(dog cat mouse)

$ echo $foo[1]

instead of

$ echo ${foo[1]}

perhaps using some shopt setting?

  • 1
    Nope. Note that in zsh. $foo[1] is dog, not cat (unless you're in sh or ksh emulation). Dec 3, 2012 at 23:37

1 Answer 1


No. If you want a shell with a lot of options that let you tune the syntax, use zsh. Bash is conservative and does not tend to implement features that would break existing POSIX or ksh scripts. Bash and ksh maintain compatibility with historical shells where $foo[1] is perfectly valid and expands to dog[1] (or dog1 if there is a file by that name).

  • To be fair to zsh, when in sh/bash/ksh mode (as in when called as "sh"/"ksh"/"bash"), that is when compatibility matters, zsh behaves like bash in this regard. Dec 3, 2012 at 23:50
  • @StephaneChazelas What I meant is that zsh has a mode where it does things its own way (plus a squillion options), in addition to its POSIX-like, ksh-like and csh-like settings. Bash hardly strays much from POSIX-like and ksh-like. Dec 4, 2012 at 0:02
  • bash is definitively implementing a lot of features, sometimes copied from zsh, but at its own pace. And bash features can break POSIX standard: this is called a bashism, it is very frequent since Ubuntu link /bin/sh to dash and it is even a regular entry in the wiktionary !
    – calandoa
    May 3, 2019 at 9:39
  • @calandoa Most places where bash is incompatible from POSIX date back from the 20th century and are preserved for backward compatibility. Most of these features are easy to avoid in scripts, so scripts written for POSIX sh almost always work in bash. A bashism is where bash extends POSIX, so scripts that rely on these features don't work with other sh implementations. May 3, 2019 at 9:55
  • @Gilles The question is not about zsh or bash conformity with POSIX syntax, it is about a new feature present in zsh and not (yet?) in bash. And btw, both zsh and bash have POSIX compatibility mode, and most non POSIX features from bash are recent: the intial version's goal was to provide a GPL version of Bourne's shell, the following versions are providing new features handy for users.
    – calandoa
    May 3, 2019 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.