Skip to main content

realpath command written in pure bash.

#!/usr/bin/env bash

#
# realpath in pure bash.
# https://github.com/dylanaraps/bareutils/blob/master/bin/realpath
#

file="${1%/}"
parent_dir="${file%/*}"

[[ -f ${PWD}/${file##*/} ]] && {
    printf '%s\n' "${PWD}/${file##*/}"
    exit 0
}

cd "$parent_dir" &>/dev/null || exit 1 &&
    printf '%s\n' "${PWD}/${file##*/}"