How do I get the full path of a relative directory in Bash?

advertisements

This question already has an answer here:

  • Converting relative path into absolute path? 6 answers

Let's say I have a variable like this:

directory="/home/lrh/package1/util/../.."

How could I convert it to the following?:

"/home/lrh"

In other words, how could I have Bash "evaluate" the directory variable and get the full path?


Changing to the directory seems to be a straightforward approach:

echo "$(cd "${directory}"; pwd)"