Bash Bash Parameter Expansion Replace pattern in string


Example

First match:

$ a='I am a string'
$ echo "${a/a/A}"
I Am a string

All matches:

$ echo "${a//a/A}"
I Am A string

Match at the beginning:

$ echo "${a/#I/y}"
y am a string

Match at the end:

$ echo "${a/%g/N}"
I am a strinN

Replace a pattern with nothing:

$ echo "${a/g/}"
I am a strin

Add prefix to array items:

$ A=(hello world)
$ echo "${A[@]/#/R}"
Rhello Rworld