Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a `##` means in bash variable substitution `${}`? [duplicate]

Tags:

bash

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!

like image 990
cmal Avatar asked Oct 31 '25 22:10

cmal


2 Answers

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).

like image 198
hnefatl Avatar answered Nov 02 '25 12:11

hnefatl


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 "*."

like image 33
Raman Sailopal Avatar answered Nov 02 '25 12:11

Raman Sailopal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!