LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-26-2013, 09:14 AM   #1
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092
Blog Entries: 1

Rep: Reputation: 90
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
 
Old 06-26-2013, 09:26 AM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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

Last edited by szboardstretcher; 06-26-2013 at 09:28 AM.
 
Old 06-26-2013, 10:03 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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
 
2 members found this post helpful.
Old 06-26-2013, 10:08 AM   #4
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Costa Rica
Distribution: Kubuntu, Debian, Knoppix
Posts: 2,092

Original Poster
Blog Entries: 1

Rep: Reputation: 90
read -r did the trick. Thank you very much.
 
  


Reply

Tags
backslashes, bash, echo, readline, sql


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 09:46 AM
bash echo variables casperdaghost Linux - Newbie 9 03-10-2010 04:21 PM
BASH: instad of echo-ing, I just want to assing to a bash variable... how?? rylan76 Linux - Newbie 9 11-28-2008 08:46 AM
(bash) echo "#!/bin/bash" event not found - trying to generate profiles automatically jimieee Programming 9 05-03-2006 10:24 AM
echo stuff i want to have returns SpeedDingo Linux - Newbie 2 05-15-2004 12:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration