How to get the full path of a file in bash?

On linux systems, you should have readlink from the GNU coreutils project installed and can do this:

readlink -f file.ext

Debian/ubuntu systems may have the realpath utility installed which "provides mostly the same functionality as /bin/readlink -f in the coreutils package."


You can use:

realpath file.ext

Instead of the pwd command, use the PWD variable (it's in POSIX as well):

fp () {
  case "$1" in
    /*) printf '%s\n' "$1";;
    *) printf '%s\n' "$PWD/$1";;
  esac
}

If you need to support Windows, recognizing absolute paths will be more complicated as each port of unix tools has its own rules for translating file paths. With Cygwin, use the cygpath utility.