tags:

views:

60

answers:

4

I have a path that is stored in a variable

$FULLPATH="/this/is/the/path/to/my/file.txt"

I also have another variable containing a partial path

$PARTIAL="/this/is/the/"

I want to remove the partial path from the full path so that I am left with:

path/to/my/file.txt

What's the best way to do this?

+2  A: 

Use bash's # pattern matching operator:

${FULLPATH#${PARTIAL}}
R Samuel Klatchko
+1  A: 

If you're sure that $PARTIAL is an actual path:

result="${FULLPATH#$PARTIAL}"
result="${result#/}"
Ignacio Vazquez-Abrams
A: 
# echo $FULLPATH | sed 's#'"$PARTIAL"'##'
janks
A: 

Here's a little more detail to Mr. Klatchko's excellent answer:

$ FULLPATH="/this/is/the/path/to/my/file.txt"
$ PARTIAL="/this/is/the/"
$ echo ${FULLPATH#${PARTIAL}}
path/to/my/file.txt
Kevin Little