símbolos especiais,. * dentro de chaves [duplicadas]

0
bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.}
echo "BASH VERSION --- $BASH_VERSION"
echo "bmajor ----- $bmajor"
echo "bminor ----- $bminor"

impressões,

BASH VERSION --- 4.2.46(1)-release
bash --- 4.2
bmajor ----- 4
bminor ----- 2

Eu normalmente uso chaves, {} para trabalhar com matrizes. Eu vejo aqui, eles são usados para correspondência de padrões.

Como esses valores, ${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.} são avaliados? E o que os caracteres especiais, * , . , # dentro de {} significam?

    
por Madhavan Kumar 07.06.2015 / 05:51

1 resposta

2

Citar do Manual de referência do bash :

  • ${parameter#word}
  • ${parameter##word}

    The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded 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. ...

  • ${parameter%word}

  • ${parameter%%word}

    The word is expanded to produce a pattern just as in filename expansion. If the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion is the value of parameter with the shortest matching pattern (the ‘%’ case) or the longest matching pattern (the ‘%%’ case) deleted. ...

por 07.06.2015 / 06:07