3

I am learning the bash script materials on http://www.tldp.org/LDP/abs/html/index.html and stuck in the Example 7-7:

http://tldp.org/LDP/abs/html/comparison-ops.html#EX14

There is an ${filename##*.} != "gz", this probably means that the $filename does not end with .gz, but I do not know the meaning of ## here. Could anyone help me?

Thanks!

cms
  • 5,864
  • 2
  • 28
  • 31
cmal
  • 2,062
  • 1
  • 18
  • 35
  • From your shell, you can also run `man bash`, then hit the forward slash to start a search, type `##` and hit Enter. Magic! :-D (And that entire section, "Parameter Expansion" is worth a close look.) – ghoti Jul 21 '17 at 09:46
  • That is holy great! @ghoti – cmal Jul 21 '17 at 10:14

3 Answers3

4

Used in a variable expansion, ${string##sub} removes the longest matching substring sub from string (# removes the shortest matching substring by contrast).

In your case, yes - this will return the string after the first . from the filename, giving the file extension.

If you search for ## in this documentation, you'll find an explanation (along with other similar commands).

hnefatl
  • 5,860
  • 2
  • 27
  • 49
  • Thanks. WIth your help, I found the guide of expansion on http://tldp.org/LDP/abs/html/refcards.html#AEN22728 . – cmal Jul 21 '17 at 09:42
  • @cmal I hadn't seen the refcards page before - that's actually really, really useful, bookmarking that. Thanks for showing me something new! – hnefatl Jul 21 '17 at 09:47
1

In the context of filenames, is trying to find the extension in the variable filename

filename="*.log"
echo ${filename##*.}
log

We are attaining the part of the string filename after "*."

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
1

## is a used for to remove a substring from a variable. For more info check this page.

For eg. if filename=/home/user.name/folder.1/test.gz, then ${filename##*.} will give you output as gz.

Lohit Gupta
  • 1,045
  • 6
  • 11