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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityDoes 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?
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).
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 !