Setting up the command line

In a previous post I went through what I install on a fresh Mac. This time I want to go through how I set up bash and a few command line tools for use in Terminal.

I have to admit that I’m a command line nerd. I grew up professionally as a C programmer under Unix System V and back then the command line was all you had. I also do a lot of work on OS X Servers where you don’t want to install too many applications and the shell can be a godsend.

Working on .bashrc

The first place to start hacking bash is in your .bashrc file. This file is run every time you start a new window. You probably don’t have one yet so go ahead and create one in your home folder.

I start by adding a CDPATH variable. This gives bash a list of places to look for a directory when you issue a cd command. Here’s mine.


This means that bash looks in the current directory, my home directory, my development directory, the usr directory and finally the top of the boot drive.

Next I want to customise the prompt. First to add some colour and second to add some more useful information.

PS1="[33[34m]h:W u$[33[0m] "

Then let’s change the colour if we run as root.

if [ `id -u` = 0 ]
    PS1="[33[31m]h:W u$[33[0m] "

Some people will recommend putting all these lines into your .profile file instead of .bashrc but if you do that then it won’t run when you do a ‘sudo -s’ to get a root shell and you won’t get the prompt change. If you want to be a traditionalist you can put everything into your .profile file and only put the change colour command into .bashrc. .profile is only run the once when you open a new Terminal window while .bashrc is run every time bash is run.

If you want to know more on customising the bash prompt there is an excellent HOWTO.

Now adjust the way bash handles the history file.

# append to the history file after every session rather than overwrite
shopt -s histappend

# prompt command is run before every primary prompt
# this one keeps the history file and list in sync
PROMPT_COMMAND='history -a; history -n'

# the above PROMPT_COMMAND can cause chaos with dups so

One of the benefits of these changes is that when you have two windows open in Terminal a shell command in one window will be accessible in the history of the other. Useful.

I don’t know about you but I like ls to list all files and use colour output so define an alias.

alias ls='/bin/ls -A -G'

Finally, for long man pages I like to read them in Preview so I define a bash function for that.

# function to send man page to preview
    man -t $* | open -f -a /Applications/

After you do that then instead of typing ‘man bash’ you type ‘manp bash’ and the bash man page will, after a short delay, open in Preview where it is much more readable.


Before we go too much further we need a package manager. The two likely culprits are MacPorts and Homebrew.

I find the Homebrew crew are better at keeping the packages up to date, it installs easily and doesn’t require any changes to your .bashrc or .profile. It is also polite in how it installs everything, creating it’s own install directory with a sub directory for each package inside it and then symlinking back to ‘/usr/local/bin’. The final advantage is that the formula are simple Ruby scripts.

Install it by copying and pasting the code below into the Terminal.

ruby -e "$(curl -fsSkL"


You may be aware that in the shell you can type part of a directory and hit tab for the shell to finish it. This is called completion.

Bash-completion is a package of scripts that extends this to the arguments of more than 150 commands, though not all of them are installed in your system.

Installing it is a two step process. First use Homebrew to install the package:

brew install bash-completion

then add these lines to your profile:

# add bash completion
source /usr/local/etc/bash_completion

and you are done.

There is a good guide to it at


That’s my basic set up for bash. On top of this I also install iPython and since Vim is my favourite command line editor I also have a .vimrc file with many years of accumulated tips but they will have to wait for another post.

If you want to learn some more then I recommend you have a close read of the bash manual. The best tutorial I know for using bash on the command line is Sam Rowe’s and it is certainly worth a read too. You will find that most bash tutorials tend to quickly turn into bash programming tutorials which you may not want.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s