Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does ## in shell script means [duplicate]

While compiling a script , I came across the command do f=${file##*/}. I am curious to know what does ## in this line means. Thanks for a reply in advance

like image 994
user1407199 Avatar asked Apr 16 '13 05:04

user1407199


People also ask

What does the fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

What Does the Fox Say video location?

Filmed at Glendora High School in Glendora, California.


1 Answers

In bash, it removes a prefix pattern. Here, it's basically giving you everything after the last path separator /, by greedily removing the prefix */, any number of characters followed by /):

pax> fspec=/path/to/some/file.txt ; echo ${fspec##*/}
file.txt

Greedy in this context means matches as much as possible. There's also a non-greedy variant (matches the smallest possible sequence), and equivalents for suffixes:

pax> echo ${fspec#*/}    # non-greedy prefix removal
path/to/some/file.txt
pax> echo ${fspec%%/*}   # greedy suffix removal (no output)
pax> echo ${fspec%/*}    # non-greedy suffix removal
/path/to/some

The ##*/ and %/* are roughly equivalent to what you get from basename and dirname respectively, but within bash so you don't have to invoke an external program:

pax> basename ${fspec} ; dirname ${fspec}
file.txt
/path/to/some

For what it's worth, the way I remember the different effects of ##, %%, #, and %, is as follows. They are "removers" of various types.

Because # often comes before a number (as in #1), it removes stuff at the start. Similarly, % often comes after a number (50%) so it removes stuff at the end.

Then the only distinction is the greedy/non-greedy aspect. Having more of the character (## or %%) obviously means you're greedy, otherwise you'd share them :-)

like image 161
paxdiablo Avatar answered Sep 29 '22 02:09

paxdiablo