1

My colleague that tested my bash script sent me this screenshot

bash5 without associative arrays

He installed bash from brew. Is it possible that declare has no -A option in bash:5 ?

I checked same with docker, bash:5 must have -A:

$ docker run -it bash:5
bash-5.0# declare -A
declare -A BASH_ALIASES=()
declare -A BASH_CMDS=()

Screenshot is similar to bash:3 output, when there is no associative arrays support:

$ docker run -it bash:3
bash-3.2# declare -A
bash: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

Is it possible to compile bash:5 without associative arrays support ?

Here is page of bash package in homebrew, I haven't found any any special options in ./configure section of Formula ruby installation script.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Paul Serikov
  • 2,550
  • 2
  • 21
  • 36
  • Could it be [this scenario](https://stackoverflow.com/a/43948526/3266847)? – Benjamin W. Mar 12 '19 at 20:06
  • As in, your script has a `#!/bin/bash` hashbang line, which hardcodes the system Bash. – Benjamin W. Mar 12 '19 at 20:07
  • Probably you are right, I know that on MacOS default bash version is 3. I'm not using MacOS, but wouldn't system Bash changed after installing via `brew` ? – Paul Serikov Mar 12 '19 at 20:12
  • 2
    The first bash in the `PATH` will be the new one, typically at `usr/local/bin/bash`, but `/bin/bash` is unchanged. – Benjamin W. Mar 12 '19 at 20:13
  • Thanks for explanation, I added `#!/usr/bin/env bash` instead of `#!/bin/bash` and direct checking version via `if [ "${BASH_VERSINFO:-0}" -lt 4 ]; then` for more portability :) – Paul Serikov Mar 12 '19 at 20:20

1 Answers1

6

To answer your question "can you compile Bash without support for associative arrays": there is a configure option --enable-array-variables, which I strongly suspect turns off support for both normal arrays and associative arrays. There is no way to turn off just associative arrays, as far as I can tell.

Much more likely, your script starts with

#!/bin/bash

which, on macOS, is Bash 3.2 without associative arrays; the fix is to use

#!/usr/bin/env bash

instead. See this answer for details and more alternatives.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116