Há apenas um novo elemento de sintaxe por linha, legal ...
Anotarei cada linha com a seção relevante de man bash
- pode ser útil como está ou em combinação com outra resposta:
Do argumento $1
, recorte 1 char começando em 0 e verifique se é um /
:
if [ ${1:0:1} = '/' ]
${parameter:offset}
${parameter:offset:length}
Substring Expansion. Expands to up to length characters of the
value of parameter starting at the character specified by off‐
set. If parameter is @, an indexed array subscripted by @ or *,
or an associative array name, the results differ as described
below. If length is omitted, expands to the substring of the
value of parameter starting at the character specified by offset
and extending to the end of the value. length and offset are
arithmetic expressions (see ARITHMETIC EVALUATION below).
If offset evaluates to a number less than zero, the value is
used as an offset in characters from the end of the value of
parameter. If length evaluates to a number less than zero, it
is interpreted as an offset in characters from the end of the
value of parameter rather than a number of characters, and the
expansion is the characters between offset and that result.
Note that a negative offset must be separated from the colon by
at least one space to avoid being confused with the :- expan‐
sion.
Deixe o caractere 0 fora e receba caracteres de 1 até o final de $1
:
tmp=${1:1} # Strip off leading '/' . . .
Veja a seção acima, primeiro caso.
Para argumentos como --foo=bar
, corte a correspondência de texto '= *' da direita, o máximo possível para a esquerda (pense em manipular --foo=bar=baz
):
parameter=${tmp%%=*} # Extract name.
${parameter%word}
${parameter%%word}
Remove matching suffix pattern. The word is expanded to produce
a pattern just as in pathname expansion. If the pattern matches
a trailing portion 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. If parameter is @
or *, the pattern removal operation is applied to each posi‐
tional 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.
Para argumentos como --foo=bar
, corte a correspondência de texto '* =' da esquerda, o máximo possível para a direita (pense em manipular --foo=bar=baz
):
value=${tmp##*=} # Extract value.
${parameter#word}
${parameter##word}
Remove matching prefix pattern. The word is expanded to produce
a pattern just as in pathname expansion. 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 pat‐
tern (the ''##'' case) deleted. If parameter is @ or *, the
pattern removal operation is applied to each positional parame‐
ter in turn, and the expansion is the resultant list. If param‐
eter 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.
(Observação: o caso de exemplo --foo=bar=baz
não é suportado como --foo
e bar=baz
, mas como --foo
e baz
)
Fonte: seção Expansão de Parâmetros em man bash
,% man bash | less '+/Parameter Expansion'
(ou mais curto man bash | less '+/##'
)