Portable way to read symlink destination in shell

51 Views Asked by At

Is there a way to portably (at least Linux and macOS) get the destination of a symlink in a (Posix) shell script? In an old program, I found

ls -lad "bin" 2> /dev/null | awk '{ printf ("%s\n", $11) }'

which however fails when user name or group name contains spaces.

stat(1) (which IMO would be the canonical solution) has different options on Linux and macOS.

0

There are 0 best solutions below