Skip to main content

Bash function to resolve the absolute path to a target file (BSD).

#!/usr/bin/env bash

#
# Resolves the absolute path of the target file
real_path () {
    local target=$1
    (
        while true; do
            cd "$(dirname "$target")" || break
            target=$(basename "$target")
            link=$(readlink "$target")
            test "$link" || break
            target=$link
        done
        echo "$(pwd -P)/$target"
    )
}

# Example usage:
$ real_path ~/projects/
# >> /Users/jon/Dropbox/dev/projects