11

My guess is that Bash is not updated on macOS. When googling update Bash macOS, I keep getting the bug fix patch. Anyway, I need to use associative arrays in macOS Bash where the command:

declare -A

yields the error:

-bash: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

I have Yosemite.

4
  • 3
    You can use home brew to get BASH 4+ version on OSX
    – anubhava
    Nov 18, 2014 at 19:20
  • Or you could use zsh, which had associative arrays first (and which is not GPL, so Apple includes newer versions of it.)
    – rici
    Nov 19, 2014 at 4:37
  • I ran into this, today, installed BASH 4.4 via Homebrew. I am impressed to see that BASH 4 is much faster than BASH 3.
    – Patrick
    Sep 10, 2017 at 22:58
  • BTW, ksh93 is another shell that supports associative arrays (predating zsh's support of same by a solid 6 years) and ships with MacOS. Jun 26, 2018 at 22:39

1 Answer 1

11

declare -A (associative arrays) are a bash 4+ feature.

The OS X bash is likely 3.X.

I don't know that OS X has an official update for bash 4+.

brew/etc. might though.

3
  • Ya I know it is, but I'm surprised that no one has had a need yet to use bash 4 on mac os x. Nov 18, 2014 at 19:18
  • 4
    @Mr.Student, it's not that nobody has a need, but Apple refuses to ship GPLv3 software, so they stick with ancient versions of bash for licensing reasons. Nov 18, 2014 at 19:20
  • after installing bash using homebrew I needed to use the bash version located here /usr/local/bin/bash
    – robbwh
    Aug 18, 2022 at 22:36

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