I have a portion of a bash script that is getting a filename without extension, but I'm trying to understand what is really going on here. What are the "%%"'s for? Can someone elaborate on what bash is doing behind the scenes? How can this technique be used on a general basis?
#!/bin/bash
for src in *.tif
do
txt=${src%%.*}
tesseract ${src} ${txt}
done
It gets rid of the filename extension (here:
.tif
), sample:from bash manual:
It's a string removal operation in the format:
${str%%substr}
Where str is the string you are operating on, and substr is the pattern to match. It looks for the longest match of substr in str, and removes everything from that point on.
Check out "Parameter Expansion" in the bash man pages. That syntax expands the $src variable deleting stuff that matches the .* pattern from it.
Apparently bash has several "Parameter Expansion" tools which include:
Simply substituting the value...
Expanding to a sub-string...
Substitute the length of the parameters value...
Expanding upon a match at the beginning of the parameter...
Expanding upon a match at the end of the parameter...
Expands the parameter to find and replace a string...
These are my interpretation of the parts I think I understand from this section of the man pages. Let me know if I missed something important.
Here's output from the bash man page