1

I am using MacOS ( Mojave 10.14.6 ) and trying to write a bash script. I have installed latest version of bash - 5.0.16(1)-release (x86_64-apple-darwin18.7.0) - using brew install.

I do not have access to update the /etc/shells file, so I tried using

#!/usr/bin/env bash
bash --version

in my script's code

And that also returns the above version - 5.0.16(1)-release (x86_64-apple-darwin18.7.0) -, but when, in the same script, I try

declare -A svcCount

I get the below error

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

which seems to indicate, the script is still using the the default bash version 3.x from Mac.

How should I change the script so that I can use the above construct of associative array definition?

Thank you

adbdkb
  • 1,897
  • 6
  • 37
  • 66
  • 1
    Make the shebang at the first line of your script `#!/usr/local/bin/bash` if you want to use the **homebrew** one. – Mark Setchell May 27 '20 at 19:29
  • I had tried that as well. Also tried it again just now, still get the same error – adbdkb May 27 '20 at 19:33
  • What is the output from `/usr/local/bin/bash --version` please? – Mark Setchell May 27 '20 at 19:37
  • 1
    Instead of `bash --version`, put `echo $BASH_VERSION` in your script. – Philippe May 27 '20 at 19:37
  • @MarkSetchell - The output /usr/local/bin/bash --version GNU bash, version 5.0.16(1)-release (x86_64-apple-darwin18.7.0) Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. – adbdkb May 27 '20 at 19:39
  • That means you are using **homebrew** bash in your script. If you then did `bash --version` you started `/bin/bash` (the Apple one) and asked it what version it is. You should have changed the first line and then tried your `declare`. – Mark Setchell May 27 '20 at 19:40
  • @Philippe - echo $BASH_VERSION shows 3.2.57(1)-release. Why is there difference and why the two commands are showing different verssions? And how can I fix it such that I can use the later version? – adbdkb May 27 '20 at 19:41
  • @MarkSetchell - Sorry, but I am a bit lost with the explanation. In the script I have the first line as - /usr/local/bin/bash - which would be bash 5, right? And if I have next line as bash --version, it also shows version 5. Even with this first line, I get the same error – adbdkb May 27 '20 at 19:46
  • Are you running it with `bash YourScript`? – Mark Setchell May 27 '20 at 19:47
  • @adbdkb, `bash --version` runs a **completely new** copy of bash, and tells you the version of that new copy. It DOES NOT tell you the version of the copy of bash that's already started. Do not use it. ONLY use `echo "$BASH_VERSION"` -- and nothing else -- when your goal is to check the version of the copy of bash that is actively running your script. – Charles Duffy May 27 '20 at 19:51
  • @MarkSetchell - Yes. Does that mean it would override the shebang version with default Mac version? – adbdkb May 27 '20 at 19:54
  • Exactly, yes. In general, if you use a shebang it tends to mean you know explicitly what shell/interpreter you want to use and you expect the user/caller to let you (the script's author) choose the appropriate one for him/her. – Mark Setchell May 27 '20 at 20:01

1 Answers1

1

Please save this as theScript if you want to use the homebrew bash:

#!/usr/local/bin/bash
echo $BASH_VERSION
declare -A svcCount

Then make it executable with:

chmod +x theScript

and run with:

./theScript
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Consider an eyebrow raised re: a non-community wiki answer to an _extremely_ oft-asked FAQ. – Charles Duffy May 27 '20 at 19:53
  • @CharlesDuffy Despite having looked, I've never understood, or been able to find anywhere, anything that tells me what a *"community wiki"* is for, or why anyone would want to make one. Who looks in such a thing? How and why? I'm happy to learn... – Mark Setchell May 27 '20 at 19:58
  • MarkSetchell - This worked. And from both yours and @Charles' explanation, I now understand where I was doing it wrong. Thanks – adbdkb May 27 '20 at 19:59
  • How do you do it with the non-homebrew version though? The built-in version. – justin.m.chase Oct 26 '22 at 14:21