Bash Function to Return the Absolute Path of a Directory Name Passed as an Argument

For some reason, a portable solution (i.e., something that works on most common flavors of POSIX systems, from the Linux variety to the Unix ones) to this is a little tricky.

Here is one that seems to do the job:

get_abs_path() {
    local PARENT_DIR=$(dirname "$1")
    cd "$PARENT_DIR"
    local ABS_PATH="$(pwd)"/"$(basename $1)"
    cd - >/dev/null
    echo $ABS_PATH

Leave a Reply

Your email address will not be published. Required fields are marked *