Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to restrain bash from removing blanks when processing file

Tags:

bash

A simple yet annoying thing:
Using a script like this:

while read x; do  
    echo "$x"  
done<file

on a file containing whitespace:

    text

will give me an output without the whitespace:

text

The problem is i need this space before text (it's one tab mostly but not always).
So the question is: how to obtain identical lines as are in input file in such a script?


Update: Ok, so I changed my while read x to while IFS= read x.

echo "$x" gives me correct answer without stripping first tab, but, eval "echo $x" strips this tab.

What should I do then?

like image 810
kasper Avatar asked Feb 17 '11 01:02

kasper


1 Answers

read is stripping the whitespace. Wipe $IFS first.

while IFS= read x
do
  echo "$x"
done < file
like image 134
Ignacio Vazquez-Abrams Avatar answered Nov 15 '22 07:11

Ignacio Vazquez-Abrams