`${parameter#word}`と `${parameter##word}`の違いは何ですか?

`${parameter#word}`と `${parameter##word}`の違いは何ですか?

によると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.

${parameter#word}なぜか他の結果が出てくる状況は見られません${parameter##word}

私は2つの文法間の異なる動作を示す状況を探しています。ところで、何ですか?最短一致パターンそして何ですか?最長一致パターン

ベストアンサー1

この例では、問題をよりよく説明できます。

$ 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}"
!

だから:

  • ${parameter#pattern}削除最短パターンマッチング
  • ${parameter##pattern}削除最長パターンマッチング

似合うパターン${parameter%pattern}もあり${parameter%pattern}終わりひも。#似たように見えたので違いを覚えています。今後 %(アメリカ)キーボードから。

おすすめ記事