You can escape your \
characters so they are still present in your read line
, like this:
echo "${lines}" | sed 's/\\/\\\\/g' | while read line; do something with ${line}; done
UPDATE: I've just updated the sed line above after some testing, try it now, it's working for me.
UPDATE 2: As it seems jordanm deleted his answer which was using a better approach than mine, I'll add it here myself:
Using the -r
parameter to the read
command, it instructs it to don't escape the slashes, so this would also work:
echo "${lines}" | while read -r line; do something with ${line}; done