If you set $CDPATH, then for goodness’ sake don’t export it. It changes the behaviour of the cd command to make it output the absolute path of the directory changed to, which breaks a common shell-scripting pattern for converting relative directory paths to absolute paths, viz:
absolute=$(cd "$relative" && pwd)
Conversely, if you’re writing a bash script and it needs to be robust against people who do export CDPATH, you can do it like this instead:
That’s a very Linux-centric view. Neither of these works on BSD. It’s probably fine if you’re writing scripts that only need to run on Linux, but I wouldn’t call it the “right method”.
Sure, readlink exists. And it lets you read symlinks. But it can’t be used to convert relative to absolute directory paths, which I thought was the use you were proposing.
> if the -f option is specified, the output is canonicalized by following every symlink in every component of the given path recursively. readlink will resolve both absolute and relative paths, and return the absolute pathname corresponding to file. In this case, the argument does not need to be a symbolic link.
This is the case for *BSD, GNU/Linux, and Irix. If macOS would prioritize being consistent with the other Nix OSs, it would be supported too, but alas Apple doesn’t.
`$()` `cd` and `pwd -P` are all part of the POSIX spec. While that doesn't guarantee they're implemented in all shells, it's a lot more likely than things like readlink which are known to not be portable with the flags required to resolve a symlink.
`readlink -f` while not POSIX proper, works on FreeBSD, OpenBSD, NetBSD, and most Linux distros. If you really need to support a wider range than that, reality says you will likely have conditional OS checks in your script anyway, so I’m not sure your point other than being pedantic?
`readlink -f` on GNU canonicalises the path.
`readlink -f` on a *BSD is for formatting the output
`readlink -f` on macOS throws an error because it's an supported option.
> if the -f option is specified, the output is canonicalized by following every symlink in every component of the given path recursively. readlink will resolve both absolute and relative paths, and return the absolute pathname corresponding to file. In this case, the argument does not need to be a symbolic link.