Substitute ./ and ../ directories by actual names

GNU coreutils has the realpath command that does just that.

/tmp/a$ realpath ..
/tmp

Though note that if the path contains symlinks, it will also resolve those:

/tmp/b/c$ realpath ..
/tmp/x/y

(Here, /tmp/b was a symlink to /tmp/x/y/)

This may be different from what the shell does with cd ... E.g. cd ../.. from /tmp/b/c in Bash shows the new path as /tmp/, not as /tmp/x.


A few ideas:

parent="$(dirname "$(pwd)")"

parent="$(
   cd ..
   pwd
)"

Tags:

Shell Script