1

I am relatively new to bash.

I had bash 3 In my mac by default.

GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

I believe that I've installed and configured bash 5 for MacOSX. Running bash -version command outputs:

GNU bash, version 5.0.3(1)-release (x86_64-apple-darwin18.2.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.

The issue is I can't execute any of the Bash 5 features like this code uses a bash 4 feature can't output the expected behavior:

for i in {1..100..2} ; do
    echo $i
done

outputs:

{1..100..2}

expected output:

1
3
5
7
9
11
13
15
17
19
21
23
25
27
29
31
33
35
37
39
41
43
45
47
49
51
53
55
57
59
61
63
65
67
69
71
73
75
77
79
81
83
85
87
89
91
93
95
97
99
Barmar
  • 741,623
  • 53
  • 500
  • 612
Hamza Belmellouki
  • 2,516
  • 1
  • 18
  • 39

3 Answers3

4

The bash -version (or bash --version) command tells you the version of Bash that you would get if you ran bash.

But I'm guessing that you're not manually running bash; rather, you're just opening the shell, which is probably still the older version of Bash. (It's quite possible, and not even that unusual, to have two copies of Bash installed on a system in different locations.) To check this, you can run echo "$BASH_VERSION" to see the version of Bash that you're actual typing in.

To fix this, you will need to configure your machine to use the newer version of Bash as your shell. (Or you can just run bash manually.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
1

If you have two versions of bash installed, you need to make sure you are running the right one. In a script you need a first line like:

#!/usr/local/bin/bash

(Adjust path as necessary for your installation.)

If you are interactive, make sure you are actually running the version of bash you expect, not just that it's the first 'bash' in your search path.

janm
  • 17,976
  • 1
  • 43
  • 61
  • 1
    Re: "If you are interactive, make sure you are actually running the version of bash you expect, not just that it's the first 'bash' in your search path": Given that `bash -version` printed the desired version, I think we can rule out any problems along those lines. – ruakh Apr 24 '19 at 15:12
  • @ruakh Why? We don't know that the login shell isn't /bin/bash, and /usr/local/bin appears earlier in the search path so that `bash --version` shows the new version. – janm Apr 24 '19 at 15:17
  • @ruakh The search path isn't used to set the interactive shell. – Barmar Apr 24 '19 at 15:18
  • @janm: Oh, I see -- I thought you were saying the opposite. My bad. – ruakh Apr 24 '19 at 15:26
1

homebrew installs programs into /usr/local/bin so if you want to use the version installed by homebrew you need to either run:

/usr/local/bin/bash --version

or ensure that /usr/local/bin occurs before /bin on your PATH, e.g.

export PATH=/usr/local/bin:PATH
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432