More than just colors: My bash settings

bash_promptIf the title sug­gests to you either a party, or vio­lence, then per­haps this post isn’t for you. If instead you are imag­in­ing a blank win­dow filled with text, read on. If you are con­fused right now, but also strangely curi­ous, you may feel free to read on.

I spend quite a bit of time inside a ter­mi­nal win­dow, doing var­i­ous things that are just eas­ier and faster than the GUI ever will be. My shell of choice is bash (by default because its what OS X and debian vari­ants ship with) and i have picked up a num­ber of use­ful login scripts that make the shell, and even the prompt itself, much more infor­ma­tive. And now i will share:

I keep all of my bash con­figs in a file in my home direc­tory called .bash_login which gets read each each a new ter­mi­nal ses­sion begins. The same can be accom­plished by adding these changes to .bashrc. If you don’t have either of them you can cre­ate the file in your home folder called .bash_login. Like so: touch .bash_login

First: Col­ors

More than just pret­ti­fy­ing the shell out­put, col­oriz­ing bash instantly adds lay­ers of infor­ma­tion to basic com­mands like ls.

# Colorize the prompt
export CLICOLOR=1
export LSCOLORS=ExFxCxDxbxegedabagacad

The first line says, well noth­ing, it’s just a label, the hash mark pre­vents bash from read­ing the con­tents of the line. But it doesn’t pre­vent you from read­ing it. See? A com­ment. The sec­ond line says “Col­orize the com­mand line inter­face? Why yes!”. And the third says what col­ors to make every­thing (believe it or not). If you’d like to tweak the col­ors your­self a bit, i’d rec­om­mend this handy color code gen­er­a­tor, because try­ing to man­u­ally adjust the LSCOLORS can be (read: is) maddening.

LS_output

The dif­fer­ent col­ors indi­cate use­ful pieces of info about your filesys­tem. Like this screen­shot of my root direc­tory. At a glance i can see file per­mis­sions, whether some­thing is a folder, an alias, an exe­cutable, and so on.

Sec­ond: History

Spend­ing as much time as i do in the ter­mi­nal, the his­tory fea­ture of bash quickly becomes invalu­able. Here are 3 com­mands i’ve added to my con­fig­u­ra­tion that i find O so helpful!

# bash history commands
export HISTCONTROL=erasedups
export HISTSIZE=10000
shopt -s histappend

Ignor­ing the com­ment line, the first com­mand tells bash to ignore dupli­cate com­mands. Really handy if you repeat com­mands in the shell a bunch, or just for ignor­ing the ones you type all the time. The sec­ond expands the default his­tory file size, i believe the default in OS X is 500 lines. I’ve upped mine to 10,000. :-) And finally, the histap­pend com­mand tells bash to append to the his­tory file on startup, over­rid­ing the default behav­ior which is to over­write (gasp!).

Added bonus: Ctrl + r will acti­vate the reverse-i-search. Try it. You can then begin typ­ing to search your his­tory file for any com­mand cur­rently stored there. Super-handy for remem­ber­ing “How exactly did i do this last time?”

Third: Your PATH

# My PATH (not a part of my spiritual journey...or is it?)
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH" 

A sim­ple one-liner, but not to be under­es­ti­mated. Your PATH is the list of direc­to­ries whose con­tents are acces­si­ble with­out hav­ing to know exactly where the com­mand is kept. I use the com­mand mysql -u root all the time. But i dont really need to know that the mysql exe­cutable resides in the direc­tory /usr/local/mysql/bin/ nor do i need to type that every time, because i’ve added that direc­tory to my PATH. Make sense? Add your own, the line is sim­ply a colon-delimited list­ing of direc­to­ries, simple.

Fourth: The prompt Lastly, but least leastly, is the prompt. The thing you stare at all the time. It’s always there. By default it’ll usu­ally tell you your user­name, host­name, and maybe your cur­rent direc­tory. With only a lit­tle bit of bash trick­ery it can be made to be super-informative.

# advanced PROMPT
PS1="\`if [ \$? != 0 ]; then echo \[\e[33m\]---=== \[\e[31m\]Oh noes, bad command \[\e[33m\]===---; fi\`\n\[\e[0;37m\][\[\e[1;31m\]\@\[\e[0;37m\]] \[\e[0;32m\]\u@\h \[\e[0;37m\][\[\e[1;34m\]\w\[\e[0;37m\]] \[\e[0;32m\]\$ \[\e[0m\] "

Gaaa! What is all that?!@? OK, all this is really say­ing is “make the bash shell spit out the time, the user­name, the host­name, and the cur­rent work­ing direc­tory. Really. O, and there’s the spe­cial invalid com­mand line in there too. Here’s what the prompt looks like:

prompt

And here’s what the error out­put looks like:

prompt_error

Many thanks to HackADay.com for the idea for this.

Lastly: Aliases

Whew, this is a big one. Prob­a­bly deserv­ing of it own article.

Here’s is my entire (abridged) .bash_login file.

# Colorize the prompt
export CLICOLOR=1
export LSCOLORS=ExFxCxDxbxegedabagacad

# Aliases
alias mystop="sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist"
alias mystart="sudo /usr/local/mysql/bin/mysqld_safe &"

# bash history commands
export HISTCONTROL=erasedups
export HISTSIZE=10000
shopt -s histappend

# My PATH (not a part of my spiritual journey...or is it?)
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH" 

# advanced PROMPT
PS1="\`if [ \$? != 0 ]; then echo \[\e[33m\]---=== \[\e[31m\]Oh noes, bad command \[\e[33m\]===---; fi\`\n\[\e[0;37m\][\[\e[1;31m\]\@\[\e[0;37m\]] \[\e[0;32m\]\u@\h \[\e[0;37m\][\[\e[1;34m\]\w\[\e[0;37m\]] \[\e[0;32m\]\$ \[\e[0m\] "

I’d LOVE to hear what other peo­ple are doing with their ter­mi­nals that they find help­ful, smart, or fun. Leave a com­ment and let me know what you know!

Thanks for fol­low­ing along every­one who made it this far. You are all get­ting gold-stars!

2 comments to More than just colors: My bash settings

  • Great stuff man, and per­fect tim­ing! Tak­ing a pro­gram­ming class that delves into some UNIX right now, so prob­a­bly noth­ing I can add to help you out. The server we play on also has a bash shell, so I am excited to try some of your wiz­ardry. Might have to pay atten­tion to face­book so I don’t miss cool stuff like this. Take it easy man, prob­a­bly see ya some­time in octo­ber. K

  • how about the python shell, or the z shell?

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>