What does shell syntax "[ ${1:0:1} = '/' ]" (parameter expansion) do?
There's just about one new syntax element per line, nice...
I'll annotate each line with the relevant section from man bash
- may be helpful as is, or in combination with another answer:
From the argument $1
, cut out 1 char starting at 0 and check it's a /
:
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.
Leave char 0 out and get chars from 1 to the end from $1
:
tmp=${1:1} # Strip off leading '/' . . .
See section above, first case.
For arguments like --foo=bar
, cut off text matching '=*' from the right, as much as possible to the left (think of handling --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.
For arguments like --foo=bar
, cut off text matching '*=' from the left, as much as possible to the right (think of handling --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.
(Note: the example case --foo=bar=baz
is not supported as --foo
and bar=baz
, but as --foo
and baz
)
Source: section Parameter Expansion in man bash
,
man bash | less '+/Parameter Expansion'
(or, shorter man bash | less '+/##'
)
This is the substring expansion construct ${parameter:offset:length}
. ${1:0:1}
takes string long one character starting at the zeroth character (the beginning of a string) of string contained in $1
- that is the first character of the forst argument of the script.
See section about parameter expansion in your shell manual page for more details.