6th January 2009

Preserve formatting when pasting in vi/vim

Thursday, July 10th, 2008

When copy and pasting into vi or vim, you can often end up with badly formatted text. This is often referred to as “the staircase effect”. It’s easily prevented by entering :set paste before you paste, when you have finished turn it off again with :set nopaste.

BASH quick tip: Change to last directory

Friday, May 16th, 2008

You can change to the previous working directory in BASH by using the command:

cd -

Parsing parameters in a BASH shell script

Monday, April 7th, 2008

This is a simple alternative to using getopts to parse parameters in a BASH shell script which makes use of the powerful parameter substitution functions in BASH. It should be sufficient for most scripts:


until [[ ! "$*" ]]; do
  if [[ ${1:0:2} = '--' ]]; then
    PAIR=${1:2}
    PARAMETER=`echo ${PAIR%=*} | tr [:lower:] [:upper:]`
    eval P_$PARAMETER=${PAIR##*=}
  fi
  shift
done

The script processes parameters in the format --name=value or --flag.

So, executing: ./example.sh --number=123 --show

Will result in the variable $P_NUMBER being set to “123″ and the variable $P_SHOW evaluating to true as it is set, albeit to a empty value.