Bash ist kein Ramsch.
Und die RegExes sind ganz normal. Kommen aber nur beim Matchingoperator =~ zur Anwendung.
Fast ganz normal.
Wie sagte mal jemand so treffend: Unix ist ein Hochhaus auf dessen Etagen je fünf verschiedene RegExFamilien wohnen. Oder so.
In der Parameter Expansion führt die Bash aber die gleiche Expansion aus, wie sie es für Dateinamen macht.
Ein ? steht zwar in RegExes für ein "Vielleicht ein Zeichen",
also optional KANN da IRGENDEIN Zeichen stehen, oder halt auch keines.
Aber eben nicht bei der Filename Expansion. Dort steht es für genau ein beliebiges Zeichen.
Und der Stern steht -wie in fast jeder RegEx Variante für Null, oder beliebig viele Zeichen.
Und damit sieht der dieser RegEx: STR="A:
<> B:<x> C:<xy>" und ersetzt das Rotgrüne mit <$SBS>
Genau, wie marce schon geschrieben hat.
Der Stern ist eben einfach greedy.
Deshalb tun beide Varianten das gleiche:
Sie ersetzen einmal den ganzen String.
Die erste Variante bricht nicht ab, sondern ist schlicht erledigt.
Und die zweiter Variante hat nach der ersten Ersetzung einfach nichts mehr was es ersetzen könnte.
Der zweite Slash steht einsam im Nichts.
Welches Problem wolltest du eigentlich lösen?
Du kannst auch Variablen als Search und Replacement verwenden.
Code:
s=something ; r=anotherthing
a="and now to something completly different"
echo ${a/$s/$r}
Aus der Manpage:
${PARAMETER/PATTERN/STRING}'
The PATTERN is expanded to produce a pattern just as in filename
expansion. PARAMETER is expanded and the longest match of PATTERN
against its value is replaced with STRING. If PATTERN begins with
`/', all matches of PATTERN are replaced with STRING. Normally
only the first match is replaced. If PATTERN begins with `#', it
must match at the beginning of the expanded value of PARAMETER.
If PATTERN begins with `%', it must match at the end of the
expanded value of PARAMETER. If STRING is null, matches of
PATTERN are deleted and the `/' following PATTERN may be omitted.
If PARAMETER is `@' or `*', the substitution 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 substitution operation is applied to each
member of the array in turn, and the expansion is the resultant
list.