Bash: Your forgotten friend (part 1)

In the name of meta-productivity, or improving productivity by improving process, I've come to learn and love Bash. For those who aren't familiar, Bash is the "scary" black terminal in Mac and Linux. Its often recognized as the home of true geeks a la Wargames. From here out, I'll assume a basic knowledge of bash (what "export" and similar commands do). In an effort to spread some of the joy of Bash, I've included a few handy features and tricks that help me out day to day.

The Prompt

The prompt is one of the first customizations that many people make. The default prompt isn't informative. You're given no information of where you are in the filesystem (ie your directory structure) or a clear idea of which user you're operating under.

The code to make this happen is segmented for readability and easy updating (I'm a fickle user, what can I say?).

# Prompt Colors
BGREEN='$\033[1;32m$'
GREEN='$\033[0;32m$'
BRED='$\033[1;31m$'
RED='$\033[0;31m$'
BBLUE='$\033[1;34m$'
BLUE='$\033[0;34m$'
NORMAL='$\033[00m$'
PS1="${BLUE}(${NORMAL}\w${BLUE})${NORMAL}\u${BLUE}@\h${RED}\{NORMAL}"


A bit of translation, the weird character combinations are what it takes to tell your terminal (the black window) how to make colors. the \w's and such, are the values you wish to show in your prompt. A complete list of these values can be found in your bash man pages (e.g. man bash at a terminal and look at the PROMPTING section).

The Output

Navigating around your filesystem with a screen-full of grey text is a quick way to get a headache. If you want to get work done, however, adding a bit of color coding can make things much easier. Bash takes this into account with the LSCOLORS environment variable. Represented in foreground/background color pairs, there are 11 values (for a total of 22 characters) which make up how your terminal outputs the color.

The designations for color and the order they go in can be seen in the man page for ls. Things are done differently between OS X and Linux, with regard to LSCOLORS. In the code example below, I have facilities for turning both from drab and grey, to informative and colorful.

If your shell doesn't do it by default, you may need to alias it to provide color by passing ls a --color flag. I go over aliases a bit more in part 2, put this should get you running.

So our .bashprofile file can be seen below.

# Better looking ls output
case uname in
Linux) # Linux Specific
export LS_COLORS="di=36;40:ln=35;40:so=32;40:pi=33;40:ex=31;40:bd=34;46:cd=34;43:su=0;41:sg=32;38:tw=0;42:ow=0;43:"
;;
Darwin) # OSX Specific
;;
*) # Non-OSX, Non-Linux.
;;
esac

alias ls="ls --color=auto"

# Prompt Colors
BGREEN='$\033[1;32m$'
GREEN='$\033[0;32m$'
BRED='$\033[1;31m$'
RED='$\033[0;31m$'
BBLUE='$\033[1;34m$'
BLUE='$\033[0;34m$'
NORMAL='$\033[00m$'

# Prompt definition
PS1="${BLUE}(${NORMAL}\w${BLUE})${NORMAL}\u${BLUE}@\h${RED}\{NORMAL}"


In part 2 of the series, I'll walk though some of the aliases I have setup which make life easier.