Definite's Extractor

My findings on Life, Linux, Open Source, and so on.

Tag Archives: bash. parameter expansion

Matching prefix pattern in Bash Parameter Expansion

According to bash manpage, when doing parameter expansion like

${parameter#word}

“The word is expanded to produce a pattern just as in pathname expansion.”

Indeed, the pathname expansion is glob expansion, however, unlike normal pathname expansion, the ‘*’ actually matches ‘/’. In other words:

PA=ja-JP/subdir/
echo ${PA##ja*}

outputs "",
instead of "/subdir/",
because ja* matches ja-JP/subdir/.

So please treat * as if it is ‘.*’ in regex.

Advertisements