DATA="a b c" ESCAPED=$(echo "${DATA}" | sed '$!s@$@@g') echo "${ESCAPED}" a b c sed "s/pattern/${ESCAPED}/" file