Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash "declare -A" does not work on macOS

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.

like image 746
Dr.Knowitall Avatar asked Nov 18 '14 19:11

Dr.Knowitall


People also ask

Does Mac use bin bash?

Calls to the POSIX sh /bin/sh in macOS are handled by /bin/bash in sh compatibility mode.

What is the declare command in bash?

'declare' is a bash built-in command that allows you to update attributes applied to variables within the scope of your shell. In addition, it can be used to declare a variable in longhand. Lastly, it allows you to peek into variables.

How do you declare in shell?

To declare a variable, just type the name you want and set its value using the equals sign ( = ). As you can see, to print the variable's value, you should use the dollar sign ( $ ) before it. Note that there are no spaces between the variable name and the equals sign, or between the equals sign and the value.

Why does Mac use old bash?

To put that in perspective, the version of Bash used on your Mac was new when the first iPhone was introduced. Apple is likely using the older version because the licensing for the Bash shell changed from GNU GPL 2 (General Public License) to GNU GPLv3, which includes restrictions that could cause problems for Apple.


1 Answers

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.

like image 174
Etan Reisner Avatar answered Sep 19 '22 09:09

Etan Reisner