prabhjot drake
`${string/substring/replacement}` replaces *first* match of `substring` with `replacement`.
How can I replace *last* match of `substring` with `replacement`?
Top Answer
Jack Douglas
Inspired by [this answer on unix.se](https://unix.stackexchange.com/a/426833), you can sandwich the replacement text between the leading and trailing parts:
::: tio S0oszvj/v7ikyDYxKdnRydnQyBjCUFDgSk3OyFdQUqkGyqoCBbVqKyqrwDxlZS0gv1bp/38A
§§§ bash bash
str=abcABC123abcABC
echo "${str%abc*}xyz${str##*abc}"
§§§
``` none
abcABC123xyzABC
```
:::