Last Updated: June 16, 2019
·
12.43K
· afterdesign

Upgrade bash on your mac os !

I do not recommend change of SHELL for root. Let's make it safe if brew upgrade will go bad.

Mac OS is using bash version 3. Or to be exact it's using /bash/sh which is:

sh-3.2# sh --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)
Copyright (C) 2007 Free Software Foundation, Inc.

Nowadays it's good to use bash v4 because it has some awesome things like associative arrays:

declare -A array
array[foo]=bar
array[bar]=foo

And more you can read in NEWS file.

So to upgrade bash to version 4 on our osx we need brew (kinda obvious).

Then we need to install bash:

brew install bash

Next we need to add our bash to shells:

sudo bash -c "echo $(brew --prefix)/bin/bash >> /private/etc/shells"

And the last step is to change our shell in system preferences so go to:

system preferences -> Users & Groups (unlock pref pane) -> right click on your account Advanced Options... and change Login shell option to /usr/local/bin/bash:

Picture

Reboot your computer and have fun with your new bash ;)

2 Responses
Add your response

Hi Rafal,

I have upgraded bash as instructed above.
echo $BASH_VERSION
is fine with response : 4.3.42(1)-release

However, the terminal still has issue
sh --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin17)
Terminal preferences setting:
Shells open with:
/usr/local/Cellar/bash/4.3.42/bin/bash

I have also done (using 'su' privilege) :
bash -c "echo $(brew --prefix)/bin/bash >> /private/etc/shells"
Do I need to fill in anything for $(brew --prefix) ?
Do I need to replace --prefix with anything etc. ?

I have restarted my MacBook, but still could not get it right.

I still have the issue with bad negative array index! Would appreciate your help.

over 1 year ago ·

@peggyli
/bin/sh and /bin/bash are still system defaults and brew will not overwrite the system defaults (and I totally agree with their approach).
If you will run which bash you should get something like /usr/local/bin/bash and this is bash installed with brew.

MacOS is using system bash version also for /bin/sh but in other unix systems you'll get different shell (for example debian is using dash as default nowadays) so you shouldn't assume that /bin/sh and /bin/bash are the same. You can have multiple shells like zsh, dash, fish, bash ;)

To run scripts with newest bash version you'll have to run in manually with bash myscript.sh. You can also set #!/usr/local/bin/bash in the top of your script and then run it without a sh/bash command like . myscript.sh

Also if you don't know it may be a good time to move to zsh as this will be the default shell in 10.15.

Cheers !

over 1 year ago ·