01 November, 2009

How to get self directory in bash script

This makes variable $SELF_DIR where is location of that script:
SELF_DIR=$(cd $(dirname $0); pwd -P)

This does the same thing as the first one but it adds the script file name to the variable:
SELF_DIR=$(cd $(dirname $0); pwd -P)/$(basename $0)

Original Post

1 comment:

  1. What about linked scripts, like init ones?
    My choice:
    SELF_DIR=$( dirname $(readlink -f $0) )