0

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.

olebole
  • 521
  • 4
  • 17
  • See [BashFAQ #29](https://mywiki.wooledge.org/BashFAQ/029), and particularly the perl solution therein; perl isn't POSIX-specified, but it _is_ pretty much ubiquitous. – Charles Duffy Jul 11 '23 at 18:56
  • Mind, Linux and MacOS is not very aggressive as a platform-support target; if those are the only platforms you care about there's `readlink` or `realpath`. – Charles Duffy Jul 11 '23 at 18:57

0 Answers0