10 (or so) bash Tricks I can’t live without
This page is a place for me to collect the configuration and usage tips I find most useful. My default .bashrc is included. I use it to get my development environment up and running quickly on a new machine. A lot of the tricks included here I have gleaned over the years from various people and web pages. Where possible I cite the original.
First thing to do is get ls to render results with colors.
# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
eval "`dircolors -b`"
alias ls='ls --color=auto'
fi
See, doesn’t that look nice?
Prompt
As a developer/admin I am constantly looking at pages of output from logs, greps, stack traces, etc… So I need a way to quickly distinguish the line on which I ran a command from the spew. I have seen people do this in a few ways some I consider overkill and some that are just downright annoying. For me, simply reversing the foreground/background colors in the first part of my prompt is enough. But why not use that visual key to convey some information? It is easy to forget that you are working on a remote machine in one shell and on your local machine in another if you use the same formatting on both. Checking to see if the shell is running in an SSH session is not hard, so I add a green ’ssh’ if it is an SSH session. Similarly, when working in a chroot enviorment I include the name of the chroot in the beginning of the prompt. Another helpful cue is to change the background color to red if the last command returned a non-zero result. That one is especially useful when cut-n-pasting a block of commands from the web and one of them fails. Finally, I include the current path so that I am not always running pwd.
#set prompt
#Error flag thanks to http://dotfiles.org/~steve/.bashrc
sh_light_red="\[33[1;31m\]"
sh_norm="\[33[0m\]"
sh_blue="\[33[34m\]"
sh_inverse="\[33[40m\]\[33[1;37m\]"
sh_inverse_red="\[33[41m\]\[33[1;37m\]"
sh_ssh="\[33[32m\]"
if [ "$SSH_CONNECTION" ] ; then
SSH_FLAG=1
#TODO get hostname from $SSH_CONNECTION
ssh_prompt=${sh_ssh}ssh${sh_norm}
else
SSH_FLAG=
fi
PS1='${debian_chroot:+($debian_chroot)}''${SSH_FLAG:+('${ssh_prompt}')}'${sh_inverse}'${ERROR_FLAG:+'${sh_inverse_red}'}\u'${sh_norm}':${SSH_FLAG:+'${sh_ssh}'}\h${SSH_FLAG:+'${sh_norm}'}['${sh_blue}'\w'${sh_norm}']${ERROR_FLAG:+'${sh_light_red}'}\$${ERROR_FLAG:+'${sh_norm}'} '
PROMPT_COMMAND='if [ $? -ne 0 ]; then ERROR_FLAG=1; else ERROR_FLAG=; fi; '
Path
A couple of bash built-ins I like are cdspell and CDPATH. cdspell will fix small typos in a path when using cd. It is good at things like reversed letters (I do that all the time) and capitalization errors. The second, CDPATH can be great, or a nightmare depending on how you set it up. I use it only for directories I am working in all the time. It can easily jump you to the wrong place if you have say a ~/devel and a ~/Documents/devel. So use with caution.
#Setup some useful bash functions
##############################################################################
#Correct small typing mistakes of cd
shopt -s cdspell
#Set CDPATH
export CDPATH=.:~:~/Documents:/mnt
History
The next two are the things that I absolutely must have. The key strokes for both are so ingrained in my work flow the I go nuts without them. I commonly find myself pressing tab to complete a word when writing an email or using up arrow to try and repeat something on skype. These key bindings for history search seem to work on any platform I have worked on, linux, bsd, mac, solaris. Though, I have yet to try them on cygwin (ick). This allows you to search the command history by typing a few chars and using up and down arrows to find commands that start with them. The history can get messy when using history search a lot, bash offers ways to control what gets written with HISTCONTROL. And, what gets written to disk when you close multiple shells? Set histappend to prevent clobbering the history file.
#Setup history searches bind '"\e[A":history-search-backward' bind '"\e[B":history-search-forward' #see: http://aplawrence.com/Linux/bash_history.html # don't put duplicate lines in the history. See bash(1) for more options export HISTCONTROL=erasedups # ... and don't clobber the history when closing multiple shells shopt -s histappend # ... and keep multi line commands together shopt -s cmdhist
Tab Completion
Bash programmable completion gives you a way to tab-complete the argument to commands. Completion for many common commands can be installed from:
http://www.caliban.org/bash/index.shtml#completion
Just put the file on your machine and source it. Or use install it from you tool of choice mac: fink, debian: apt, redhat: rpm, etc...
###############################################################################
#Use bash-Completion
if [ -f /sw/etc/bash_completion ]; then
. /sw/etc/bash_completion
fi
Referances
When I started writing this I looked for what others have written and learned a few things. Here are some of the pages I liked.
Shell colors: Tip: Prompt magic
Got cmdhist from here: 5 ways to make using bash more productive
Lots of tips: Power Shell Usage Bash Tips & Tricks


