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.

codeforester
  • 39,467
  • 16
  • 112
  • 140
Dr.Knowitall
  • 10,080
  • 23
  • 82
  • 133
  • 3
    You can use `home brew` to get BASH 4+ version on OSX – anubhava Nov 18 '14 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 '14 at 04: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 '17 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. – Charles Duffy Jun 26 '18 at 22:39

1 Answers1

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.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • 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. – Dr.Knowitall Nov 18 '14 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. – Charles Duffy Nov 18 '14 at 19:20
  • after installing bash using homebrew I needed to use the bash version located here ```/usr/local/bin/bash``` – robbwh Aug 18 '22 at 22:36