Tech Tutorial – Adding BASH 4 to OSX Using Home BrewOS X
Fun With OS X
– Installing BASH 4 on OS X using Home Brew
If you have spent any time using LINUX or even other Unix variants like Solaris, you are already painfully aware of the differences that exist in various shells, commands, and utilities. One of the more striking differences is that Apple continues to include BASH v.3.2.57 which is about 8 years old and does not support some very import features of v4 such as associative arrays and a myriad of additional types of command completion. For a full list of BASH 4 changes click HERE. (Replace HERE with this LINK https://www.admon.org/scripts/new-features-in-bash-4-0/). The reason that Apple does not include a newer version of BASH is that BASH4 is now under the GPL3 license, and does not conform to Apples copyright philosophy. However this does not stop you from installing it yourself.
There are several ways to install BASH 4 on your Mac, including compiling from source if you are feeling masochistic. The method that I used, installing the Home Brew repository will be covered in this tutorial.
Part I – Installing the Homebrew repository.
If you use Linux, you are already familiar with a software repository, and package management software. Red Hat and CentOS uses yum and rpm, while Unbunto Linux uses apt-get or Synaptic. For us OS X users, the only software respository most of us are familiar with is the App store, which allows us to purchase, install software and manage their updates. Homebrew is basically an App Store for software packages created, compiled, and maintained by the community and are free of charge. This is where we will be obtaining BASH version 4. We first though must install XCode Command Line Tools (CLT), and Homebrew.
For detailed instructions to installing Homebrew, go to brew.sh
- Launch your terminal
2. Copy and paste the shell command on the home page into your terminal window at the shell prompt.
This will automatically install XCode Command Like Tools, and the homebrew repository.
3. Once installed test by typing man brew in your terminal window.
Part II – Installing BASH 4
1. In your shell window switch to an account with elevated privledges, brew will not execute being run using sudo
$ su – <priledged username>5
Then type.
$ brew install bash bash-completion
This will install both BASH 4 and BASH completion in /usr/local/bin/base
2. We now need to add the new shell to the list of whitelisted shells, this is done in the /etc/shells file.
$ echo “$(brew --prefix)/bin/bash” >> /etc/shells
3. OPTIONAL Change your login shell to BASH4
$ chsh –s $(brew --prefix)/bin/bash
NOTE: I chose to leave my login shell at BASH3, as BASH4 can be invoked in the shebang #! at the beginning of any BASH4 script. Here are some other personal tweaks I did for my BASH4 installation.
I created a symbolic link for /bin/bash4 to /usr/local/bin/bash. That way I can execute BASH v4 scripts using #!/bin/bash4.
I did this by typing the following
$ ln -s /usr/local/bin/bash /bin/bash4
You can see that bash4 is a link to /usr/local/bin/bash
I can change to BASH4 from any shell in this manner.
I can re-execute the login script next by entering the following command.
$ source /etc/profile
PART III – BASH Version checking in login scripts.
I also added a BASH version checking routine to be placed in /etc/profile (Login script that is executed when opening a terminal window for ALL USERS).
You may want to consider adding this to your profile script.
3 ## BASH3 .profile (Put ALL BASH3 specific code here)
4 function bash3 {
5 echo -e “\nUsing BASH 3 Specific and Version Neutral login profile\n”
6 }
7
8 ## BASH4 .profile (Pull ALL BASH4 specific code here)
9 function bash4 {
10 echo -e “\nUsing BASH 4 Specific and Version Neutral login profile\n”
11 }
12
13 # Default BASH version checking
14
15 if (( ${BASH_VERSION%%.*} > 3 ))
16 then
17 bash4
18 else
19 bash3
20 fi
21
22 ## PUT ALL VERSION NEUTRAL BASH SCRIPTS BELOW THIS LINE!
That is all there is to installing BASH4 in OSX. I hope you enjoyed the tutorial will keep returning to Brent’s world for more great articles!
Comments
Tech Tutorial – Adding BASH 4 to OSX Using Home BrewOS X — No Comments
HTML tags allowed in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>