Ruby, JavaScript, Sass, iOS. Stinky Cheese & Beer Advocate. Working at CustomInk and loving it!

Getting On Good Terms With The OS X Shell

iTerm Logo I will be the first to admit that I am really just learning how to tap into the power of my shell environment and to be honest, I've spent way to many hours reading man pages and figuring out how to do some really neat things that help my automate my workflow and system administration. Mostly these are just basic tasks like my Simple MySQL Backup and Deleting Invisible Resource Files scripts. But in all seriousness, when you get right down to using a UNIX-based operating system, you cannot escape using the shell environment. This is a good thing, its your friend, and getting your feet wet sooner than later is a good idea.

Getting A Better Terminal Application

Go download iTerm from the sourceforge project page and install it. That default Terminal.app that comes with the Mac has got to go, it is dreadful looking and the features are really slack. Fortunately iTerm is a native Cocoa app that supports both the PowerPC and the new Intel Macs. The best feature of iTerm is that it supports tabbed windows, because you can never have to many terminal windows open. Here are some preferences I recommend after installing iTerm.

  • Tab Position Top
  • Selection copies text to clipboard & Focus follows mouse
  • The "Foreground" and ANSI Colors pictured below, most are defaults.
  • A dark background image with little detail so as not to interfere with your foreground text.
  • In there "Terminal" tab of the profiles, set the "Type" to "xterm-color".
iTerm Profile Preferences

Customizing Your Bash Profile

After you have a nice terminal program, its time to tweak your environment a bit. Here is a great set of customization commands that will change the way your shell interface is displayed when using the bash terminal, this is the standard shell environment on newer versions of OS X. Using these environment enhancements you will get a solid visual on the following information which is also colored coded to appear well on a dark background.

  • The current user you are logged in as (green) for you (red) for root/others.
  • The current machine name and working directory.
  • A new entry line which is not obscured by the items up above.
# User colors (root colors in /etc/bashrc)
export TERM="xterm-color"
C1="\[\033[1;32m\]"
C2="\[\033[1;30m\]"
C3="\[\033[0m\]"
C4="\[\033[0;36m\]"
export PS1="${C2}(${C1}\u${C2}@${C4}\h${C2}) - (${C4}\A${C2}) - (${C4}\w${C2})\n${C2}-${C1}=>>${C3}"
# Root colors (user colors in ~/.bash_profile)
export TERM="xterm-color"
C1="\[\033[0;31m\]"
C2="\[\033[1;30m\]"
C3="\[\033[0m\]"
C4="\[\033[0;36m\]"
export PS1="${C2}(${C1}\u${C2}@${C4}\h${C2}) - (${C4}\A${C2}) - (${C4}\w${C2})\n${C2}-${C1}=>>${C3}"

Here is how to install these, they go into two separate places. The first customization commands go into your user home directory in a file called .bash_profile and if you have never modified or used this file, you will have to make a new one. The period that precedes the name is technically not allowed by most text editors since it makes the file invisible. I suggested using TextMate to create or modify this and the file below. Remember, the first file .bash_profile needs to go into your home directory. The more you use your terminal, the more you will start to modify this file. It too is your friend.

The second set of customization commands go into the root bashrc file. Again, using TextMate, you can find this file in the /etc directory. If you are new to using TextMate to get to files in invisible directories, do this. When you have the "Open..." dialog window in view in TextMate, hold down Command-Shift+G. This will open a sub dialog window called "Go to the folder:" with a text field to type the directory structure into. Simply enter /etc and hit the "Go" button. This will take you right to the /etc directory and in there you should see the bashrc file. Open it add the lines from the second code sample box above and save it. From this point onward your terminal window in iTerm should look something like this.

My iTerm Window

I never setup a server without putting these customization settings in place. It just makes me feel more at home on the command line. In some later articles, I'll start to cover some really sweet shell scripts for more workflow automation. Let me know if you like this or if you have any problems setting them up on your system.