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 $0to get the canonical path.dirnameutil is useful here.BASH_SOURCEis better than$0, as$0gives the user's typed in command, which might not be the currently executing script.