This site uses cookies. By continuing, your consent is assumed. Learn more

124.9fm shares

Bash absolute path from relative dating

opinion

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

For what it's worth, I voted for the answer that was picked, but wanted to share a solution.

The downside is, it's Linux only - I spent about 5 minutes trying to find the OSX equivalent before coming to Stack overflow. I'm sure it's out there though.

You're safe if you're targeting a directory, basename will return nothing and you'll just end up with double slashes in the final output.

The initial question was very confused to start with, with an example poorly related to the question as stated.

The selected answer actually answers the example given, and not at all the question in the title. The first command is that answer is it really? And I fail to see what the second command is doing.

One may or may not want to resolve explicity such symlinks. This may or may not yield an absolute path name, depending on how it is done.

And one may, or may not want to resolve it into an absolute pathname. The command readlink foo without option gives an answer only if its argument foo is a symbolic link, and that answer is the value of that Bash absolute path from relative dating. No other link is followed.

The answer may be a relative path: However, readlink has options -f -e or -m that will work for all files, and give one absolute pathname the one with no symlinks to the file actually denoted by the argument.

This works fine for anything that is not a symlink, though one might desire to use an absolute pathname without resolving the intermediate symlinks on the Bash absolute path from relative dating. This is done by the command realpath -s foo.

In the case of a symlink argument, readlink with its options will again resolve all symlinks on the absolute path to the argument, but that will also include all symlinks that may be encountered by following the argument value. You may not want that if you desired an absolute path to the argument symlink itself, rather than to whatever it may link to.

Again, if foo is a symlink, realpath -s foo will get an absolute path without resolving symlinks, including the one given as argument.

absolute path for a script...

Without the -s option, realpath does pretty much the same as readlinkexcept for simply reading the value of a link, as well as several other things. It is just not clear to me why readlink has its options, creating apparently an undesirable redundancy with realpath.

News feed