Take the 2-minute tour ×
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.

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

$ foo=(dog cat mouse)

$ echo $foo[1]
cat

instead of

$ echo ${foo[1]}

perhaps using some shopt setting?

share|improve this question
1  
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

1 Answer 1

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

 
discard

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.