Unexpected sed result.

I am in the process of writing a script to change the grub password in the grub.conf file. I thought I had it figured out, but am running into an a problem I can't put my finger on.

Command I am running when I find that the grub.conf file contains "password --md5".

sed "s=password.*=password --md5 "$grubPW"=g" /boot/grub/grub.conf

If I run it from the shell prompt it works as expected, but when I put it into my script it doesn't replace the search string, it does add the replacement string though.

I switched the traditional '/'s to "="s because the hashed password will sometimes contain slashes.

Expected Result:
password --md5 $1$5fKefK.fw/FeMM4d2D

Actual Result:
password --md5 $1$5fKefK.fw/FeMM4d2D
password --md5

I should just end up with the one line containing the hash value.

Your MD5 password contains sed and shell metacharacters, which may be interpolated in your script because $grubPW is unquoted. Quote the entire sed expression to protect the shell from interpreting the value of $grubPW. The shell will variable expand inside of double quotes:

sed "s=password.*=password --md5 $grubPW=g" /boot/grub/grub.conf

If this still has troubles, show the script segment that allows us to test the failure.