31 ms·
That's a portable sh solution, isn't it? Like, yes, it does work on FreeBSD, but it isn't exclusive
by yjftsjthsd-h 3y ago
That's a portable sh solution, isn't it? Like, yes, it does work on FreeBSD, but it isn't exclusive
- oefrha 3y agoPOSIX has no realpath(1), so no.
- arp242 3y ago*Yet. Will be added in the upcoming revision: https://www.austingroupbugs.net/view.php?id=1457 https://www.austingroupbugs.net/view.php?id=1457 20 years ago it was a "BSD thing", but it seems to be more wide-spread these days; even my Alpine busybox system has it. I'm sure there are some platforms without it, but there are also platforms without some POSIX tools – it's never a guarantee.
- pixelbeat__ 3y agoAll the main platforms have realpath(1). It was added to GNU Linux over 10 years ago now, to aid portability, and be a more natural command to access this functionality than readlink(1)
- oefrha 3y agoNot really. macOS until version 12 doesn’t have realpath(1), it was only added this year. You can’t rely on it for portable code.
- latexr 3y agoTo clarify, it was added (late) last year, with macos 13 (Ventura).
- inferiorhuman 3y agoSaw a comment on another site mention this, but NetBSD won't get it until 10.0 (not yet released) and OpenBSD got it in 7.1 (released last year). Of course I'm of the mindset that building complex shell scripts is simply an exercise in masochism so this all seems like a solution in search of a problem.
- racingmars 3y agoI think the closest portable solution we have would be basename instead of realpath, but I don't think that takes care of the symlink problem described in the article.
- mhio 3y agoThe portablest solution I have found is: rundir=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)")
- HerrMonnezza 3y agoI'm curious: why the `printf '%s\n' "$(pwd -P)"` ? wouldn't just `pwd -P` achieve the same?
- alganet 3y agoI've been using just `V="$(cd -- "$(dirname -- "$0")"; pwd)"` for ages, everything quoted, several OSs, never had an issue with the output that required me to format it again. This approach breaks if you run the script from a symlink.
- mhio 3y agoActually nothing. The format seems to have come from a similar line that builds the script path.. printf '%s/%s\n' "$(pwd -P)" "$(basename "$0")" It's just been in place, in so many places, so long it's become unquestionable. So yes the `pwd -P` in this case will suffice.
- account42 3y agoThis isn't a portable solution at all because it doesn't solve the problem of the script being symlinked. While it does resolve directory symlilnks, that part is usually not even needed as finding a helper works just as well with a symlink to the directory containing the script as it does with the resolved directory. What doesn't work is a directory containing a symlink to the script which your "solution" fails to resolve. Additionally (and the article doesn't solve this either), when invoked through $PATH, $0 will not be a full path but only the name of the command. In that case you want to start with "$(command -v "$0")" instead of just "$0".
- Asooka 3y agoNo, $0 is a full path when invoked via $PATH.