.bashrc by example

The learning curve for .bashrc is very broad and takes patience or a very good tutorial. Here’s a happy medium between those two.

Basics:
Bash is a shell that allows you to enter commands at the command line. It’s a very robust program that allows scripting, variables, and a whole host of other features you may/may not ever use. If you’re interested in learning about it, there are a lot of great books on the subject.

If you don’t know what shell you are running on Mac OS X or if you’re new to Linux, try opening a Terminal window and typing this:

echo $SHELL

That will tell you the full path of the shell program you are using. For instance, mine is ‘/bin/bash’.

Bash reads from several files in a prescribed order, depending on various circumstances. This can allow for customization or be very confusing, depending on how sophisticated you want to be. I’m only now getting a sense of why that could be useful, but let’s ignore that for now. I tell bash to basically look at one file for all my shortcuts, etc., so that’s what we’ll set up here.

Three files:
To do this, in our home directory (~/), we have to create the files: ‘.profile’, ‘.bash_profile’. You can do this using pico, vim, emacs, or any other Text Editor. They are going to be identical, and all they will contain is the following line:

source ~/.bashrc

And now, in our home directory (~/), we will create ‘.bashrc’. Here’s mine, annotated with comments:


# The # sign notes that the line is a comment
# v 0.3.2m # I always version to make sure I am not overwriting files.
# 2009-02-13 # This is the last date I edited a file.

# PS1,PS2, etc. are variables that allow you to customize your command prompt.
# More can be found on this by searching the internet
# Mine tells bash to give me the host name (\h), the highest directory of my working path (\W)
# my user name (\u) and the version of the shell (\v), followed by $, which is a sign used
# to denote that this is not a super user
# It ends up looking like:
# [unimind : ~] sc 3.2$
# PS1="[\[33[0;37m\]\h : \W] \u \v$ "
PS1="[\h : \W] \u \v$ "

# bash's vi mode - this allows me to use vi like commands in bash. I think this is
# actually a readline feature. Search for 'bash' and 'set' for more info.
# set -o vi

# screen stuff from hans.fugal.net
# This is an environment variable PROMPT_COMMAND that does some
# sneaky things for the program GNU Screen.
export PROMPT_COMMAND='echo -ne "\ek\e\\"'

# up and down keys search through history like in MATLAB
# bind '"\e[A": history-search-backward'
# bind '"\e[B": history-search-forward'

# more history stuff
# HISTSIZE sets the number of history items to retain but I commented it out for now
# HISCONTROL=ignoredups tells bash not to register two instances of a command in a row
# export HISTSIZE=1000
export HISTCONTROL=ignoredups

# setting ctrl+L behavior to clear screen
bind -m vi-insert \C-l:clear-screen

# GS_OPTIONS are my ghost script options to not compress raster images with epstopdf
export GS_OPTIONS="-dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode"

# set editor variable for general editing
export EDITOR=vi

# MATLAB path information? doesn't appear to work
export MATLABPATH=~/.matlab/R2008a/

# Python startup file
export PYTHONSTARTUP=~/.pythonrc

# PATH is a variable that is tricky. You probably shouldn't use this version but the following:
# export PATH=$PATH
# you can add other paths to this by using the colon notation seen below.
export PATH=/bin:/sbin:/usr/X11/bin:/usr/X11R6/bin:/opt/bin:/opt/local/bin:/usr/bin:/usr/sbin:/usr/local/bin:/Developer/usr/bin/:/Applications/matlab/bin

# Aliases for my sanity
# these let me type the alias instead of the entire command for common commands
# the semicolon separates different commands in bash
alias app='open -a' # usage: 'app itunes' will open itunes, mac only
alias current='cd ~/sandbox/codeproject/current_branch/; pwd'
alias data='cd /repo/data/; pwd; ls'

# common/overloaded util names
# I do this because i want these programs to always run in these modes
alias mv='mv -iv'
alias cp='cp -iv'
alias df='df -h'
alias eject='hdiutil eject' # usage: 'eject /Volume/LIBRARY' will unmount and eject LIBRARY
alias ls='ls -p'
alias unmount='hdiutil unmount' # usage: 'unmount disk3s1' will unmount disk3s1
alias ip='ipconfig getifaddr en0'
alias sys_prefs='open -a System\ Preferences'

# title creation function
# I don't ever use this but it shows that many other things are possible in .bashrc
function title ()
{
case $TERM in
*term | ansi | xterm-color | rxvt |vt100 | gnome* )
echo -n -e "33]0;$*07" ;;
*) ;;
esac
}

# specific program paths
# more aliases
alias matlab='matlab -nodesktop -nosplash'
alias matlabnj='matlab -nojvm'
alias matlabh='matlab -nojvm -display $DISPLAY'
alias matlab64='/Applications/matlab64/bin/matlab -nodesktop -arch maci64'
alias neuron='/Applications/NEURON-5.9/nrn/i686/bin/nrngui'
alias gmail='open -a google\ notifier'
alias preview='open -a preview'
alias vpn='open -a /Applications/Shimo.app/'
alias xterm2='xterm -geometry 80x60+575+5 &'

# QuickLook from the command line
# Display files in Quick Look
function ql ()
{
(qlmanage -p "$@" > /dev/null 2>&1 &
local ql_pid=$!
read -sn 1
kill ${ql_pid}) > /dev/null 2>&1
}
# Display any filetype as plain text
function qlt ()
{
(qlmanage -p -c public.plain-text "$@" > /dev/null 2>&1 &
local ql_pid=$!
read -sn 1
kill ${ql_pid}) > /dev/null 2>&1
}

1 thought on “.bashrc by example

  1. Pingback: Python Startup on Mac OS X Leopard « consider a spherical cow

Leave a comment