Convert absolute path into relative path given a current directory using Bash

absolute-path, bash, path, relative-path, shell

Solution

Using `realpath` from GNU coreutils 8.23 is the simplest, I think:

$ realpath -s --relative-to="$file1" "$file2"

For example:

$ realpath -s --relative-to=/usr/bin/nmap /tmp/testing
../../../tmp/testing

The `-s` flag ensures that symlinks are not expanded.

Problem

Example: ``` absolute="/foo/bar" current="/foo/baz/foo" # Magic relative="../../bar" ``` How do I create the magic (hopefully not too complicated code...)?

Original source