开发者

Getting a partial path to a file in a bash script

I ha开发者_Go百科ve 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?


Use bash's # pattern matching operator:

${FULLPATH#${PARTIAL}}


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


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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜