4

I'm getting this error declare: -A: invalid option while declaring the following map

declare -A ApiResourcePath_LambdaFunc_Map=( \
    [signup]="CreateUser" \
    [verify-user]="VerifyUser" \
    [login]="Login" \
    [change-password]="ChangePassword" \
    [forgot-password]="LostPassword" \
    [reset-password]="ResetPassword" \
    [refresh-token]="RefreshToken" \
)

I didn't find any syntax error here. Why I am getting this error ?

Rui F Ribeiro
  • 55,929
  • 26
  • 146
  • 227
Rafaf Tahsin
  • 281
  • 3
  • 9
  • 1
    What OS are you on? What is the output of `bash --version`? – John1024 Mar 05 '18 at 08:34
  • See also:[Bash 4 associative arrays: error “declare: -A: invalid option”](https://stackoverflow.com/questions/6047648/bash-4-associative-arrays-error-declare-a-invalid-option). – John1024 Mar 05 '18 at 08:37
  • 1
    @John1024 Thanks John, you are absolutely right. I was running bash version 3 in macOS. bash 4 supports the above script but bash 3 doesn't. – Rafaf Tahsin Mar 05 '18 at 08:47
  • This may also be helpful: https://unix.stackexchange.com/questions/111397/associative-arrays-in-shell-scripts – Kusalananda Mar 05 '18 at 09:36

1 Answers1

7

Support for associative arrays was added to bash in version 4.0-alpha. For whatever reasons, macOS is still using bash 3.2. If you want to use associative arrays on macOS, one option is to install a modern bash using, for example, Homebrew.

For more on this and the work-around options for macOS users, see StackOverflow.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
John1024
  • 73,527
  • 11
  • 167
  • 163
  • The reason macOS is still using version 3.2 is because bash 4 is licensed under GPLv3, which Apple cannot use. For more -https://apple.stackexchange.com/questions/193411/update-bash-to-version-4-0-on-osx – Rafaf Tahsin Mar 05 '18 at 09:46