Nohsib Nohsib - 25 days ago 6
Linux Question

How to convert ".." in path names to absolute name in a bash script?

How to convert the

..
in the path names to absolute path names in a bash script. That is, if I have a path
/home/nohsib/dvc/../bop
, I want this to be changed to the path without dots in it, in this case
/home/nohsib/bop


How can I do that?

Answer

What you're looking for is readlink:

absolute_path=$(readlink -m /home/nohsib/dvc/../bop)

Please note: You need to use GNU's readlink implementation which offers the "-m" option. BSD's readlink for example does not.

Comments