cb0
cb0

Reputation: 8633

Reading file line by line in Bash expands special characters

I'm parsing some .html files in Bash. I read the input with:

while read line 
do
   echo $line
   ...do something...
done < $file

Now I've expierenced something real strange. Some lines in the files contain something like

Resolution…: 720 * 576

But bash gives me this:

Resolution…: 720 mysript.sh another_script.pl 576

Bash expands the * char to the content of the actual directory. How can I read the text line by line without expanding.

Upvotes: 1

Views: 4028

Answers (2)

ghostdog74
ghostdog74

Reputation: 343067

you should read your file like this, with the -r option

while read -r line 
do
   echo "$line"
   #..do something...
done < "$file"

Upvotes: 5

catwalk
catwalk

Reputation: 6476

the expanding happens in echo, not in read, you should quote your output:

echo "$line"

Upvotes: 9

Related Questions