Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Nope. Note that in zsh. $foo[1] is dog, not cat (unless you're in sh or ksh emulation). – Stéphane Chazelas Dec 3 '12 at 23:37
up vote 4 down vote accepted

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

share|improve this answer
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. – Stéphane Chazelas Dec 3 '12 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. – Gilles Dec 4 '12 at 0:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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