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

Viewed 24858

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?

10 Answers

Relative paths must be converted into absolute, it is NOT required to exist!

Neither symlinks should be resolved.

Try this one-line python solution:

abs_path=$(python -c "import os; print(os.path.abspath(\"$rel_path\"))")  

in your example:

python -c "import os; print(os.path.abspath(\"/home/nohsib/dvc/../bop\"))"  

returns /home/nohsib/bop

Related