0

I've been working with bash more and more on my Mac and have discovered that for some reason the declare -A command does not work.

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

This answer says that support for associate arrays was added to bash in version 4.

When I run bash --version I get 5.0.11.

myname$ bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
  [1]: https://unix.stackexchange.com/a/428205/379464

I ran bash --version and echo $BASH_VERSION as @chepner recommended and they give different answers.

bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
SEGON207127:gnubin nberkowitz$ echo $BASH_VERSION
3.2.57(1)-release

I've already run brew install gnutils and added it to PATH in .bashrc.

Anything else I should try?

lespaul
  • 477
  • 2
  • 8
  • 21
  • 2
    You have both versions of bash installed side by side. You need to make sure that whatever you're calling `declare -A` from is running with the correct version – that other guy Jan 08 '20 at 18:39
  • 2
    Are you sure you are trying to use `declare -A` in `bash` 5, rather than the default `/bin/bash` (which is only version 3.2 and does not support `-A`)? – chepner Jan 08 '20 at 18:40
  • 2
    It's possible that `/bin/bash` is still your login shell, even if the first `bash` in your `PATH` is your version 5. `bash --version` runs `bash` again, rather than reporting the version of the current shell. Use `echo $BASH_VERSION` to confirm your login shell is 3.2. – chepner Jan 08 '20 at 18:41
  • 1
    By the time your `.bashrc` runs, the shell *is already loaded*, so nothing you do there can change the version that's in use (unless you have it start a completely different executable, discarding per-process state that isn't reflected in the environment; I do not advise that approach). – Charles Duffy Jan 08 '20 at 18:54
  • 1
    ...what you really should be doing is configuring your OS to run the shell you really want directly -- `/usr/local/bin/bash` instead of `/bin/bash`, f/e -- as your account's login shell. As others have said before, `bash --version` just tells us which version is first in the PATH, which has nothing whatever to do with which version is currently running. – Charles Duffy Jan 08 '20 at 18:55
  • @CharlesDuffy is this true if you are sourcing your .bashrc at the top of your .bash_profile? – lespaul Jan 08 '20 at 19:09
  • @lespaul, the same thing is true of `.bash_profile`; it itself is sourced by an interpreter which is necessarily already running before the process of reading the file starts. – Charles Duffy Jan 08 '20 at 19:10
  • A very hacky approach of trying to have the interpreter replace itself might look something like `[[ $BASH_VERSION = 3.* && -x /usr/local/bin/bash ]] && exec /usr/local/bin/bash "$-"`, but I don't advise doing that -- there's a lot that can go wrong in ways that are hard to repair (if you can't start a shell in which to repair your shell!). – Charles Duffy Jan 08 '20 at 19:11
  • @CharlesDuffy LOL I see what you're saying, very much a chicken/egg problem and you don't want to kill all your chickens and toss out all of your eggs. Is there a cleaner, less risky way to do this? If there is one I'll happily accept it as the answer. – lespaul Jan 08 '20 at 19:14
  • As I suggested above, better to configure your OS to start the shell you want. `chsh` or whatever replacement it prefers, to point at `/usr/local/bin/bash` or wherever else the newer copy of bash you want to actually use is installed. `sudo chsh -s /usr/local/bin/bash "$USER"` f/e (`sudo` is needed to make `chsh` accept a shell not named in `/etc/shells`; `USER` should by default refer to your current user account, which in this case is expected to be the one for which you intend to switch shells). – Charles Duffy Jan 08 '20 at 19:19
  • ...see https://superuser.com/questions/48226/how-do-i-set-my-shell-in-mac-os-x – Charles Duffy Jan 08 '20 at 19:19

0 Answers0