In a script I get in $0
the possible relative path to it. For converting it to absolute I've found this solution which I don't understand:
abspath=$(cd ${0%/*} && echo $PWD/${0##*/})
My problem is the magic inside of ${0%/*}
and ${0##*/}
. It looks like the former extracts the dirname and the latter extracts the filename, I just don't get how.
readlink -f $0
to get the canonical path.dirname
util is useful here.BASH_SOURCE
is better than$0
, as$0
gives the user's typed in command, which might not be the currently executing script.