BASH: The difference between the two paths?

Let's say I have ways

a/b/c/d/e/f
a/b/c/d

      

How do I get below?

e/f

      

+3


source to share


1 answer


You can separate one line from another:

echo "${string1#"$string2"}"

      

Cm:

$ string1="a/b/c/d/e/f"
$ string2="a/b/c/d"
$ echo "${string1#"$string2"}"
/e/f

      

From man bash

shell extension options :

$ {parameter # word}

$ {parameter ## word}

The word expands to create a template in the same way as the filename extension. If the pattern matches the beginning of the extended parameter value, then the expansion results in the extended parameter value with the shortest matching pattern (case "#"), or the long pattern matching (case ##) is removed.




With spaces:

$ string1="hello/i am here/foo/bar"
$ string2="hello/i am here/foo"
$ echo "${string1#"$string2"}"
/bar

      

To "clear" a few slashes, you can follow Roberto Reale's suggestion and canonicalize paths with readlink -m

to provide comparisons to strings with the same real path:

$ string1="/a///b/c//d/e/f/"
$ readlink -m $string1
/a/b/c/d/e/f

      

+12


source







All Articles