PWD variable is set by the shell to one absolute location of the current directory. Any component of that path may be a symbolic link.
case $f in
If you want to eliminate
.. as well, change to the directory containing the file and obtain
if [ -d "$f" ]; then f=$f/.; fi
absolute=$(cd "$(dirname -- "$f")"; printf %s. "$PWD")
There's no portable way to follow symbolic links. If you have a path to a directory, then on most unices
$(cd -- "$dir" && pwd -P 2>/dev/null | pwd) provides a path that doesn't use symbolic links, because shells that track symbolic links tend to implement
pwd -P (“P” for “physical”).
Some unices provide a utility to print the “physical” path to a file.
- Reasonably recent Linux systems (with GNU coreutils or BusyBox) have
readlink -f, as do FreeBSD ≥8.3, NetBSD ≥4.0, and OpenBSD as far back as 2.2.
- FreeBSD ≥4.3 has
realpath (it's also present on some Linux systems, and it's in BusyBox).
If Perl is available, you can use the
perl -MCwd -e 'print Cwd::realpath($ARGV)' path/to/file