Using sed I need to replace variables in an HTML document.

The following is what i am using:

cat $email_temp\
 | sed 's/\$message_entry/Flex File Export completed successfully for'"$run_date"'./g'\
 | sed 's/\$run_date/'"$run_date"'/g'\
 | sed 's/\$flex_file_new_count/\'"$flex_file_new_count"'/g'\
 | sed 's/\$exported_file_count/'"$exported_file_count"'/g'\
 | sed 's/\$date_data/\'"$date_data"'/g'\
 | sed 's/\$flex_file_old_count_data/\'"$flex_file_old_count_data"'/g' > $email_body
  • 1
    It's hard to say what you're doing wrong when we don't know what you're trying to do. Please edit your question and describe what you're trying to do, and how what you've tried behaved differently than what you want. Nov 4, 2019 at 1:03
  • 1
    You have mismatch because of ' character.
    – unxnut
    Nov 4, 2019 at 1:23
  • Does run date include any slashes?
    – Mikel
    Nov 4, 2019 at 1:25
  • 1
    Whenever you inject stuff externally u need to vet them carefully. Check if your variable is having newlines. Especially $flex_file_new_count. Nov 4, 2019 at 14:58

1 Answer 1


As pointed out in the comments by @unxnut, there are three escaped single quotes \' in your command which should not be escaped. Thus sed cannot find the matching closing single quote and fails.

Try this (with quoted variables $email_temp, $email_body and one sed call):

sed -e 's/\$message_entry/Flex File Export completed successfully for'"$run_date"'./g'\
 -e 's/\$run_date/'"$run_date"'/g'\
 -e 's/\$flex_file_new_count/'"$flex_file_new_count"'/g'\
 -e 's/\$exported_file_count/'"$exported_file_count"'/g'\
 -e 's/\$date_data/'"$date_data"'/g'\
 -e 's/\$flex_file_old_count_data/'"$flex_file_old_count_data"'/g' "$email_temp" > "$email_body"
  • Thanks for help but it didn't work for me ... i am still getting error sed: -e expression #3, char 24: unterminated `s' command Nov 4, 2019 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.