Problems with ampersand (&) in sed command

Hello everybody,

I have a Problem with sed command.
I want to replace a defined string with a string from a database field (dynamic).

e.g.

sed -i -e 's/%NAME%/'"$HNAME"'/g'

The Problem is that the $HNAME variable can contain Special characters like '&'
e.g.

HNAME="AH Kruger & Co. KG"

so I get the Output

"AH Kruger %NAME% Co. KG"

sed replace the ampersand with the searchstring.

Is it possible to tell sed not to modify the replacestring?

Thx for your help... :slight_smile:

PS: Sorry for my bad english...

You'd need to escape the & character. In a recent shell this could be done like

sed "s/%NAME%/${HNAME//&/\\&}/g" file

.