DATA="$(cat whatever)" ESCAPED_DATA="$(echo "${DATA}" | sed ':a;N;$!ba;s/ / /g' | sed 's/$/$/g')" Then you can use ${ESCAPED_DATA} in sed: cat input | sed 's/one liner/'"${ESCAPED_DATA}"'/' > output