LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash - how to keep echo from replacing backslashes stuff? (https://www.linuxquestions.org/questions/programming-9/bash-how-to-keep-echo-from-replacing-backslashes-stuff-4175467466/)

eantoranz 06-26-2013 09:14 AM

bash - how to keep echo from replacing backslashes stuff?
 
Hi!

I'm reading a SQL input with readme in order to do some processing with the lines and then I use echo to write them into STDOUT. Something like this:

Code:

IFS=''
while readline line; do
    # I do some processing
    echo "$line"
done

I tried echo -e and -E and with both the echo removes a "\\" and writes "\" and it should write "\\".

Thanks in advance

szboardstretcher 06-26-2013 09:26 AM

You can always use cat...

Code:

inputfile="$(cat < testfile)"

for line in $inputfile
do
 echo -E $line
done

Code:

testfile:
\\what
\\\what
\what

Code:

output:
>for line in $inputfile
> do
>  echo -E $line
> done
\\what
\\\what
\what


ntubski 06-26-2013 10:03 AM

It's not echo that is removing the slashes, but rather read. A solution is to use read -r:

Quote:

read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name …]
...
-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.


Quote:

Originally Posted by szboardstretcher
You can always use cat...

Ooh, David the H. is going to be so mad :tisk: :p

eantoranz 06-26-2013 10:08 AM

read -r did the trick. Thank you very much.


All times are GMT -5. The time now is 04:46 AM.