Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep backslash when reading from a file?

Tags:

bash

When I use "cat test.file", it will show

printf "This is a test log %d \n, testid";
 1
  2

When I use the bash file,

IFS=""
while read data
do
    echo "$data"
done << test.file

It will show

printf "This is a test log %d n, testid";
 1
  2

The "\" is gone.

Is there any way that I can keep the "\" and space at the same time?

like image 545
paler Avatar asked Sep 07 '11 01:09

paler


Video Answer


1 Answers

Try using read -r.

From the man page:

-r
If this option is given, backslash does not act as an escape character. The backslash is considered to be part of the line. In particular, a backslash-newline pair may not be used as a line continuation.

Execute this to test it:

read -r a < <(echo "test \n test"); echo $a
like image 61
Lynch Avatar answered Sep 19 '22 05:09

Lynch