View previous topic :: View next topic |
Author |
Message |
psy_ill Tux's lil' helper
Joined: 06 Jul 2003 Posts: 106
|
Posted: Fri May 20, 2005 8:41 am Post subject: Converting relative path to absolute path in Bash |
|
|
This is a neat trick which has its uses if you are dealing with softlinks. I needed it just the other day, and it took me a while to figure out how to achieve it. If someone else has a better way to do this, I would be glad to see it, as I find my own solution a bit clumsy.
Code: |
# Convert relative paths to absolute
ABSOLUTEPATH=$(cd `dirname "$RELATIVEPATH"` && pwd)"/"`basename "$RELATIVEPATH"`
|
The script above has a drawback though; the trailing slash if you try to convert a path to a directory. Still, it will function. |
|
Back to top |
|
|
zmedico Developer
Joined: 02 Jan 2004 Posts: 352 Location: California USA
|
Posted: Fri May 20, 2005 9:38 am Post subject: |
|
|
readlink -f has a similar function. The canonical path is often as good as any absolute path. The bash builtin "cd" has a -P parameter that would make your code behave like readlink -f. _________________ Zac |
|
Back to top |
|
|
psy_ill Tux's lil' helper
Joined: 06 Jul 2003 Posts: 106
|
Posted: Mon May 23, 2005 10:27 am Post subject: |
|
|
Ah, nice. I thought readlink only operated on softlinks.
Still, the trick is useful when you haven't got readlink, for example at the sun systems at my university. |
|
Back to top |
|
|
slycordinator Advocate
Joined: 31 Jan 2004 Posts: 3065 Location: Korea
|
Posted: Mon May 23, 2005 4:04 pm Post subject: |
|
|
An easier way is to just do:
note that those are back-quotes/back-ticks. It's found on the key to the left of the number 1.
This tells it to use the system command pwd rather than the built-in bash command. |
|
Back to top |
|
|
frenkel Veteran
Joined: 13 May 2003 Posts: 1034 Location: .nl
|
Posted: Mon May 23, 2005 4:09 pm Post subject: |
|
|
slycordinator wrote: | An easier way is to just do:
note that those are back-quotes/back-ticks. It's found on the key to the left of the number 1.
This tells it to use the system command pwd rather than the built-in bash command. |
Thanks! Great tip. |
|
Back to top |
|
|
Gherald Veteran
Joined: 23 Aug 2004 Posts: 1399 Location: CLUAConsole
|
Posted: Mon May 23, 2005 4:11 pm Post subject: |
|
|
|
|
Back to top |
|
|
|