As arguments to my script there are some file paths. Those can, of course, be relative (or contain ~). But for the functions I've written I need paths that are absolute, but do not have their symlinks resolved.
Is there any function for this?
Join Stack Overflow to learn, share knowledge, and build your career.
|
As arguments to my script there are some file paths. Those can, of course, be relative (or contain ~). But for the functions I've written I need paths that are absolute, but do not have their symlinks resolved. Is there any function for this? |
||||
|
Consider this as well (source):
|
|||||||||||||||||
|
|
http://www.linuxquestions.org/questions/programming-9/bash-script-return-full-path-and-filename-680368/page3.html has the following
which uses |
|||||||||||||
|
|
Simple one-liner:
Usage:
I am still trying to figure out how I can get it to be completely oblivious to whether the path exists or not (so it can be used when creating files as well). |
|||||||||||||||||||||
|
|
on OS X you can use
on linux you might have
|
|||||||||
|
|
This does the trick for me on OS X: It should work anywhere. The other solutions seemed too complicated. |
|||
|
|
|
Maybe this is more readable and does not use a subshell and does not change the current dir:
|
|||
|
|
|
self edit, I just noticed the OP said he's not looking for symlinks resolved: "But for the functions I've written I need paths that are absolute, but do not have their symlinks resolved." So guess this isn't so apropos to his question after all. :) Since I've run into this many times over the years, and this time around I needed a pure bash portable version that I could use on OSX and linux, I went ahead and wrote one: The living version lives here: https://github.com/keen99/shell-functions/tree/master/resolve_path but for the sake of SO, here's the current version (I feel it's well tested..but I'm open to feedback!) Might not be difficult to make it work for plain bourne shell (sh), but I didn't try...I like $FUNCNAME too much. :)
here's a classic example, thanks to brew:
use this function and it will return the -real- path:
|
|||
|
|
|
Do you have to use bash exclusively? I needed to do this and got fed up with differences between Linux and OS X. So I used PHP for a quick and dirty solution.
I know it's not a very elegant solution but it does work. |
|||
|
|
readlinkresolves symlinks; the OP doesn't want that. – Keith Thompson Aug 19 '11 at 19:54echo $(cd some_directory && pwd), do not resolve symlink, fails if some_directory not exist. working directory not affected. or assign to a variableMY_PATH=$(cd some_directory && pwd). – qeatzy Nov 22 '17 at 12:50