Tom Sweeney
Tom Sweeney

Reputation: 640

Getting a partial path to a file in a bash script

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?

Upvotes: 1

Views: 1199

Answers (4)

Kevin Little
Kevin Little

Reputation: 12976

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

Upvotes: 3

janks
janks

Reputation: 2158

# echo $FULLPATH | sed 's#'"$PARTIAL"'##'

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799150

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

result="${FULLPATH#$PARTIAL}"
result="${result#/}"

Upvotes: 1

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76581

Use bash's # pattern matching operator:

${FULLPATH#${PARTIAL}}

Upvotes: 3

Related Questions