You can use any of:
x=a/b/c/d
y=a/
echo ${x#a/}
echo ${x#$y}
echo ${x#*/}
All three echo commands produce b/c/d
; you could use the value in any way you choose, of course.
The first is appropriate when you know the name you need to remove when writing the script.
The second is appropriate when you have a variable that contains the prefix you need to remove (minor variant: y=a; echo ${x#$y/}
).
The third is the most general - it removes any arbitrary prefix up to the first slash. I was pleasantly surprised to find that the *
worked non-greedily when I tested it with bash
(version 3.2) on MacOS X 10.6.6 - I'll put that down to too much Perl and regex work (because, when I think about it, *
in shell doesn't include slashes).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…