Shell variables in sed script

The sed command works as expected at the command prompt, but does not work in a shell script.

new_db_name=`echo "$new_db_name" | sed 's/$replace_string/$replace_with/'`

Why is that, and how can I fix it?


Solution 1:

Use double quotes for the sed expression.

new_db_name=$(echo "$new_db_name" | sed "s/$replace_string/$replace_with/")

Solution 2:

If you use bash, this should work:

new_db_name=${new_db_name/$replace_string/$replace_with}