0

In El Capitan, 10.11.5 the Bash version is:

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

How can I use / install Bash 4.3 in order to utilize features such as the one listed below?

How to get a colored tab completion?

3

Note: This is really not a difficult process. There are just a lot of words. It probably takes about a total of 2 minutes or less depending on your system. You can almost copy/paste these commands in sequence. Just change anything that is in all CAPS, usually.

I just did this the other day for macOS 10.12.x. I went with v4.3.30 since it looks like v4.4 is still being tested, or only recently finished.

Download it (v4.3.30 in this case) from the source: http://ftp.gnu.org/gnu/bash/bash-4.3.tar.gz

Once you've got bash downloaded, you can pretty much run these commands in sequential order, if you're okay with installing at ~/Bin (which is my suggestion; you can always move it later). Otherwise, I'll point out where you can change this. Everything prefixed with a "#" is a comment, so don't enter it at the command line!

    # Unpack the archive.
    tar zxvf ~/Downloads/bash-4.3.tar.gz 
    # Change into the directory (of the unpacked archive).
    cd ~/Downloads/bash-4.3/
    # Create a directory to store some of your user programs.
    mkdir ~/Bin
    # Run configure which will gather info. about the installation 
    #+ environment. Optionally, you can include the --prefix option along
    #+ with an argument for the install location. If you'd like to install  
    #+ elsewhere, use --prefix and the location of your choosing.
    ./configure --prefix ~/Bin
    # Compile the source code.
    make
    # Install the compiled program. 
    # Note: You can do without sudo since you're installing locally.
    sudo make install

That's it. bash is now installed. However... There are a few things, you should know:

You now have two different versions of bash installed on your system. This is perfectly fine. It is even desirable for testing and backwards compatibility. This is what I'm doing, and I plan to continue. It's just like having a differently named shell on your system, like zsh, sh, csh, etc.

The thing to be aware of is that when you launch the Terminal app and open a new shell, you're default shell is still going to be the old bash, unless you change it (more on that below). To launch the new bash, you can't just enter the command bash because then bash is going to look at your PATH variable and go down the list and try to find bash, and it's most likely going to come up with /bin/bash before it finds your new bash, especially since the new bash might be (if you followed my advice) in a folder called ~/Bin which isn't even a part of your PATH, yet.

So, at this point, if you want to run bash-4.3.30 from the command-line, enter the full path, replacing USERNAME with the name of your home directory: /Users/USERNAME/Bin/bash/bin/bash And you can do a bash --version and say, "Hey, wtf! Why am I still running the old bash?" And that's because you've invoked the old bash! Once again, entering the command bash is going to more than likely get resolved to /bin/bash. So, how can we fix this? Or, at least, how can we see that we are in fact running bash-4.3.30?

If you want to know which shell you're running from the command line, do: echo $0

$0 is a special parameter for bash, that stores the name of the executing process, which in this case, is bash-4.3.30 (the one who is doing your bidding on your behalf, usually executing other programs for you). Now, to see the version, enter the full path to the proper program you want to interpret that argument, which is the new bash-4.3.30, and is the same as the value of $0 which you just got back from echo. So, do: /Users/USERNAME/Bin/bash/bin/bash --version to see the difference. It might seem weird, but you are running bash-4.3.30 right now, but you're running it as a child process within /bin/bash (the old bash). Now type exit and you'll exit out of bash4.3.30 and be back in the old bash. exit again and you'll more than likely close the terminal window and logout of the shell session.

Now, you've got a few different options as for making use of bash-4.3.30 without having to enter the full path every time you want to launch it.

If you want to run bash-4.3.30 by default, you can change your default login shell in the Terminal.app: Preferences… > General > Shells Open With > ...Change this to Default Login Shell and enter the new complete path of bash-4.3.30, changing USERNAME to the name of your home directory: /Users/USERNAME/Bin/bash/bin/bash

If you'd still prefer to run the old bash at login, but just make it a little easier to launch the new bash by not having to remember the path, you could just create an alias and append it to your ~/.bashrc: echo 'alias bash4="/Users/USERNAME/Bin/bash/bin/bash"' >> ~/.bashrc This way, you can invoke bash-4.3.30 by just typing bash4 from the command-line.

Alternatively, you could add the location of the new bash to your PATH variable. If you prefix it, rather than append, you should get bash-4.3.30 when you invoke bash from the command-line:

    # Prefix ~/Bin/bash/bin/ to your PATH variable:
    PATH="~/Bin/bash/bin:${PATH}"
    # Save it by changing it in your ~/.bashrc
    echo 'export PATH="$PATH"' >> ~/.bashrc

Just remember to do echo $0 if you're not sure which bash you're running. I wouldn't recommend renaming the new or old bash. This could potentially lead to undesirable consequences. Though, I did it on my system as a test (it requires sudo). This is how you learn! And, now you've always got the old bash to fall back on, and you know how to build a new bash from the source all over again, if need be.

2
  • 1
    Great resource. Two notes: this installed to ~/Bin/bin/bash (no extra bash folder), and it seems the easier way to set it as default is to run chsh -s ~/Bin/bin/bash
    – dlsso
    May 19 '17 at 17:15
  • @disso Yes, that's where I had installed it originally. But as I've learned more about Unix-based systems, I'd now recommend installing it to /usr/bin as this is where most user binaries go. Then, as you said, use chsh to change your default shell, e.g., if installed to /usr/bin do: chsh -s /usr/bin/bash Thanks for clarifying and pointing out the use of chsh, which seems imperative on macOSX, as you're not "allowed" to edit the global config files where this is set.
    – GH05T
    May 26 '17 at 0:53
1

You could use a package manager (Homebrew, MacPorts, Fink, Nix, Rudix, etc.) to install a Bash binary, or you could compile from source if you don't use a package manager and prefer not to install one.

Homebrew, MacPorts, and Nix have Bash packages >= Bash v4.3. I'm not sure about other package managers. If you don't want to install via a package manager, you could download the Bash 4.3 source, then compile it. If you are unsure how to do this, please feel free to ask me for help. I will happily step through the process with you. (Edit: GH05T's answer details how to compile from source)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.