Last Updated: February 25, 2016
·
716
· ericmarcos

Shell script good practice

Quite often I find myself writting shellscripts that have to deal with relative paths. The problem is that you can't assume that the user of your script is executing it from the scripts directory, so if you want something like this:

#!/bin/bash
./some/other/script.sh

to work from everywhere, you'll have to cd into your script's directory first:

#!/bin/bash
BASEDIR="$( cd "$( dirname "$0" )" && pwd )"
cd $BASEDIR
./some/other/script.sh