0

So when coding a bash/sh script I got a path in a variable with the following content:

$SOMEPATH=/usr/lib64/pkgconfig/../..

How can I convert $SOMEVAR to a full absolute path that converts the ".." folders to this value?:

$SOMEFULLPATH=/usr

Thanks

2 Answers 2

4

You could use readlink:

readlink -f "$SOMEPATH"
   -f, --canonicalize
          canonicalize  by  following  every symlink in every component of
          the given name recursively; all  but  the  last  component  must
          exist

   -m, --canonicalize-missing
          canonicalize by following every symlink in  every  component  of
          the  given  name recursively, without requirements on components
          existence
Sign up to request clarification or add additional context in comments.

Comments

1

Similar to the answer from @devnull I would recommend readlink, however the -m option does not require any part of the file path to already exist, which is more flexible.
readlink -m $SOMEFULLPATH

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.