Sourabh Saxena Sourabh Saxena - 1 year ago 65
Bash Question

Reading file line by line (with space) in Unix Shell scripting - Issue

I want to read a file line by line in Unix shell scripting. Line can contain leading and trailing spaces and i want to read those spaces also in the line.
I tried with "while read line" but read command is removing space characters from line :(
Example if line in file are:-

abcd efghijk
abcdefg hijk

line should be read as:-
1) "abcd efghijk"
2) " abcdefg hijk"

What I tried is this (which not worked):-

while read line
echo $line
done < file.txt

I want line including space and tab characters in it.
Please suggest a way.

sat sat
Answer Source

Try this,

while read line
echo $line
done < file.txt


From man bash

IFS - The Internal Field Separator that is used for word
splitting after expansion and to split lines into words
with  the  read  builtin  command. The default value is