Bash last index of
bash, pattern-matching, shell
Solution
$ a="artifact-1.2.3.zip"; a="${a#*-}"; echo "${a%.*}"
‘`#`pattern’ removes pattern so long as it matches the beginning of `$a`. The syntax of pattern is similar to that used in filename matching. In our case,
- `*` is any sequence of characters.
- `-` means a literal dash.
- Thus `#*-` matches everything up to, and including, the first dash.
- Thus `${a#*-}` expands to whatever `$a` would expand to, except that `artifact-` is removed from the expansion, leaving us with `1.2.3.zip`.
Similarly, ‘`%`pattern’ removes pattern so long as it matches the end of the expansion. In our case,
- `.` a literal dot.
- `*` any sequence of characters.
- Thus `%.*` is everything including the last dot up to the end of the string.
- Thus if `$a` expands to `1.2.3.zip`, then `${a%.*}` expands to `1.2.3`.
Job done.
The man page content for this is as follows (at least on my machine, YMMV):
${parameter#word}
${parameter##word}
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 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}
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 pat-
tern (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.
HTH!
EDIT
Kudos to @x4d for the detailed answer. Still think people should RTFM though. If they don't understand the manual, then post another question.
Problem
Sorry for the lame bash question, but I can't seem to be able to work it out. I have the following simple case: I have variable like `artifact-1.2.3.zip` I would like to get a sub-string between the hyphen and the last index of the dot (both exclusive). My bash skill are not too strong. I have the following: ``` a="artifact-1.2.3.zip"; b="-"; echo ${a:$(( $(expr index "$a" "$b" + 1) - $(expr length "$b") ))} ``` Producing: ``` 1.2.3.zip ``` How do I remove the `.zip` part as well?