change the filename by adding up 1 each time, tricky one

I have undestood the used syntax (with help from another person)

and replay to my question by myself.
It is parameter expansion POSIX shell parameter expansions

And that means:

suffix=${name#*.}
  • # remove shortest part from beginning for matching *. - so for name="abc.def.gh" it removes the "abc." and returtns "def.gh", although "abc.def." is also matching the *.
    So, this statement means - everything after first dot.
main=${name%.$suffix}
  • % remove shortest part from end for matching .$suffix - So, it is removing suffix with dot from end, and returns beginning before .

Shorter it could be done with the same result by:

main=${name##*.}
  • but the suffix is used in final construction
    The double % and # ( %% and ## ) means to remove longer matching, instead of shorter

Now the

prefix=${main%%+([0-9])}

means : get part before longer line of consecutive digits on end of the 'main'
: '+' - one or more repetition; [0-9] - any digits

and

num=${main#$prefix}
  • obviose is that just removed in previose statement string of digits

The statement num=10#${num} adds the base of the number. Othervise the '09' would be treated as 8-base number and rase an error (9 - is out of 0-7 digits)
Acctualy, last two statement before final file name construction could be done in that construction:

newname=${prefix}$((10#$num+1)).${suffix}

The '$((' and '))' define arithmetic calculation, '10#' before variable annonce the base for number.