-2

According to man bash:

${parameter#word}  
${parameter##word}
    Remove matching prefix pattern. The word is expanded to produce a
    pattern just as in pathname expansion, and matched against the
    expanded value of parameter using the rules described under Pattern
    Matching below. If the pattern matches the beginning of the value of
    parameter, then the result of the expansion is the expanded value of
    parameter with the shortest matching pattern (the ``#'' case) or the
    longest matching pattern (the ``##'' case) deleted. If parameter is
    @ or *, the pattern removal operation is applied to each positional
    parameter in turn, and the expansion is the resultant list.  If
    parameter is an array variable subscripted with @ or *, the pattern
    removal operation is applied to each member of the array in turn,
    and the expansion is the resultant list.

Somehow, I don't manage to find a situation that gives different results between ${parameter#word} and ${parameter##word}.

I am looking for situations that would illustrate the different behaviours between the two syntaxes. And incidentally, what would be a shortest matching pattern and what would be a longest matching pattern?

2 Answers 2

2

This example may be more illustrative

$ parameter="This is a sentence with many words. Some of the words appear more than once. Oh, my word!"
$ echo "${parameter#*word}"
s. Some of the words appear more than once. Oh, my word!
$ echo "${parameter##*word}"
!

So:

  • ${parameter#pattern} removes the shortest match of the pattern
  • ${parameter##pattern} removes the longest match of the pattern

There are also ${parameter%pattern} and ${parameter%pattern} that match the pattern at the end of the string. I remember the difference because # appears before % on a (US) keyboard.

2
  • Unfortunately, on a german keyboard this hint does not work. I always have to look it up ...
    – pLumo
    Sep 15, 2021 at 6:03
  • Ah! I would have expected a minimal match of '*word' to be with just 'word', which of course is not a match because the string doesn't start with 'word'. However 'This' is indeed the minimal match obtained with 'This*' as a pattern... Nicely illustrative, thank you. Somehow I couldn't wrap my mind around the process from just reading the man page. But now that I know what it explains it all makes sense. Btw, the keyboard hint works on a French keyboard.
    – The Quark
    Sep 15, 2021 at 6:20
1

I just found this example:

$ INT=00011
$ echo "${INT#*0}"
0011
$ echo "${INT##*0}"
11

Here, we first assign the string "00011" to the variable 'INT', then we output the result of the parameter expansion performed by either ${parameter#word} or ${parameter##word} with 'INT' as the parameter and '*0' as the word pattern.

The minimal [prefix] match of '*0' with the parameter is just '0', whereas the maximal [prefix] match is '000'.

1
  • 1
    It'd be helpful to others in the future to add some explanation of your example. Sep 14, 2021 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.