There surly is a better way to write that, but by bash foo isn't good enough. This one was picked from stackoverflow.