Reputation: 267
This always returns nothing: readlink $Somevar
. I can have "${Somevar}"
, "$Somevar"
, $Somevar
, or ${Somevar}
- it makes no difference. I keep printing Somevar
and I know it has a sane value. Just entering $Somevar
at the terminal (once I echo/"debug" my script to give me the relative path and abort) gives me the absolute path. But I need to be able to get the absolute path within the script!
Is it possible at all to use readlink in conjunction with a variable? I frankly have no idea how readlink even "knows" that it is being passed a variable. I thought even in a script it would be expanded by the shell before readlink was called. I've tested it in the terminal (sans scripts) with "~/_src
". The only way it works is if I write that with no quotes. But if I assign that value to a variable every other function on my machine works with it BUT readlink.
How can I get the absolute path? I don't even care if I use readlink. I just want to get the absolute path from relative in a script.
UPDATE: (Stackoverflow comments broken)
I tagged as Linux. I asked for readlink because I also want to translate symlinks, but I am fine doing that in a separate step.
Also, this fails as well: ABSDIR=`cd $RELDIR; pwd`
gives
bash: cd: ~/_src/: No such file or directory
(Which there is such a directory, and cd
will find it if I type that exact dirname on the terminal.) I am now guessing that ~/
is expanded by the terminal itself.
So, is there no way to handle paths like that? Apprently all the utilities I thought handled these paths, were only working because the terminal expands the path before calling them.
Upvotes: 3
Views: 6148
Reputation: 4974
On Cygwin, I have the same behaviour, so I append some readlink
flag, such as -f
, and it works fine:
readlink -f $Somevar
Upvotes: 2
Reputation: 198436
First of all, readlink does something slightly different on Linux or on OSX. Tagging your question with an OS rather than bash
is a better idea, especially since readlink
is not a bash
builtin, but rather an executable.
Secondly, if you want to get an absolute path, why is that not the title of your question?
The easy and portable way to get an absolute path of a directory is
ABSDIR=`cd $RELDIR; pwd`
Files are a little more work:
ABSFILE=`cd $(dirname $RELFILE); pwd`/`basename $RELFILE`
Upvotes: 5