A Bash upgrade on Mac OS X

Mac OS X offers one of the most advanced user experiences, a modern operating system can offer. It’s user interface is unique, beautiful designed and very intuitive. But above all Mac OS X is a strong and hundred percent POSIX-compliant UNIX. Not everyone knows about this affinity, but you can believe me: nothing beats the power of a Unix command line. OS X provides a terminal just like Linux does, and no limits are set. To unfold it’s might, all you have to do is open your utilities folder and start Terminal.app. There are numerous shells around, Mac OS X takes advantage of one of the most popular shells out there: Bash. Sad but true, Snow Leopard does not come with a recent version. With simplicity and some great tools we will change that.

The Snow Leopard version of Bash (Bourne again shell) is 3.2.48, currently we experience stable 4.0.x releases. Of course said branch has a lot more features and options. It is required to have Xcode installed for this tutorial, as we need to compile a new Bash version from sources and therefore we need Apples version of GCC (GNU Compiler Collection), part of the Xcode Developer Tools. You can download the latest release from here. No Linux user would ever live without a package manager and that is exactly what we need next. There are two interesting projects to mention, Fink and Macports. My favorite is Macports, because it has the most recent packages. After installing Macports you could do everything from the command line. Accessing Macports is very easy with “port” command. For example:

sudo port selfupdate

would update Macports to the most recent version. Yes, you can do a sudo on OS X. This task needs administrative rights, so in fact you need to sudo. But I will introduce you to another wonderful free tool now, it’s called Porticus and a native Cocoa GUI for the Macports package manager, similar to Synaptic which acts as front end to apt-get on Ubuntu Linux. Grab yourself Porticus and the gate is open to thousands of great open source tools, all available as ports for your favorite Unix.

Porticus

Porticus handles nearly every command flag you can do with Macports itself. Now search for Bash and install it, note that Macports will automatically install all dependencies for this port. The installation window gives you the opportunity to select which port variant you want to install. I run Snow Leopard, so I have not chosen to install the universal variant. Everyone with an Intel Mac and Leopard (or above) should do the same, as it saves your valuable disk space. Compilation without universal flag on Snow Leopard results in a Intel Mac native 64-bit binary. Good to know where Macports installs it’s goodies, you’ll find them all at /opt/local/bin. Macports added that path to .bash_profile at your home directory. The file .bash_profile can be edited to customize Bash to your needs, I will give some examples later. The next step is to make the newly installed Bash version the standard shell on your system. Run Terminal.app and open the settings, you should select the first tab “Start”.

Terminal settings

Don’t mind this screenshot is in German, it’s my system locale and I was too lazy to switch. Let’s select under “shells open with” the option “command” and enter the path /bin/bash as seen on the picture. Next we will backup the original Bash version shipped with Snow Leopard and replace it with a symbolic link, pointing to our brandnew Bash in the Macports directory. We’ll use Terminal.app to navigate to /bin. I won’t explain basics like this further, if you don’t know them, my tutorial is definitely useless for you. Use the following commands in /bin dir to backup Snow Leopard’s Bash build and to create the necessary symbolic link:

sudo mv bash bash-3.2.48 

sudo ln -s /opt/local/bin/bash bash

You can check if you did the right thing by typing:

ls -lahf bash*

Bash 4 OS X symlink

Take a look at the two Bash entries, you should see something similar to the screenshot above. Bash is a symbolic link, pointing to the file installed by Macports. Quit Terminal.app and restart it. Use the following command to display the Bash version information:

bash —version

Bash 4 on Mac OS X

A brandnew Bash for your Mac OS X terminal, with lots of features. Enjoy. One last thing: I promised to give some examples in how you can use .bash_profile in your $home directory to customize the Bash shell to your needs. I’m sure you recognized the difference between your “ls -lahf” command and mine. My output is colored, known (and probably loved) on every Linux terminal. We’ll need to edit your .bash_profile to achieve it on Mac OS X. Open a new terminal, you should already be in your home directory. A “ls -h” will show all files, even those files Finder hides from your eyes. Now do the following to edit .bash_profile:

nano .bash_profile

If you don’t like GNU Nano and prefer VIM as editor, of course, that’s no problem. Just edit the file with whatever you want. In .bash_profile add the following lines to make it work:

# ls colored output for dark backgrounds

export CLICOLOR=true export LSCOLORS=dxfxcxdxbxegedabagacad

Note that this will only look good on darker backgrounds. If you prefer a brighter background in terminal (I don’t), use this:

[bash]# ls colored output for bright backgrounds

export CLICOLOR=true export LSCOLORS=ExFxCxDxBxegedabagacad

From now on colors should work. But maybe you want to do another tweak before we leave this place. In it’s standard configuration on OS X, the LS command does not have a sorted and human readable output. I don’t like that and it’s very easy to change. We’ll make an alias for the LS command, and yes, we’ll do it in .bash_profile, too. Just add the following lines:

# system specific tweaks

alias ls=”ls -ahf”[/bash]

Be sure to save your changes, quit and restart Terminal.app. A simple LS command wil now have an identical, beautifully sorted output : 

LS colored and sorted

Of course, everything is colored. Any “ls -l” will have the same effect you previously had with “ls -lahf” above. I hope I was able to introduce some great tools and practices for a better terminal experience on OS X. Feel free to contact me if you have any additions or you found out something’s not working.

28 notes

Show

  1. byteproject posted this