Bash string replacement - how to use regex?

Hello

I have a bash script where I need to do a substring replacement like this:
variable2=${variable1/foo/bar}

However, I only want "foo" replaced if it is at the end of the line.

However, this does not work:
variable2=${variable1/foo$/bar}

as you can see I'm using the $ regex for end of line.

Does anyone know what the correct syntax is?

Thanks

Hi, try:

variable2="${variable1%foo}bar"

This is not regex by the way....

That works.

Many thanks.