I have a very simple scripts called test.sh
.
#!/bin/bash
echo $(cat \'./data\')
while read line
do
data=$data\' \'$line
done < \'./data\'
echo
Rule #1, quote your variables!
echo "$(cat data)"
and
data="$data $line"
and
echo "$data"
Although as gniourf_gniourf pointed out in the comments below (since removed), the second of the three changes is actually unnecessary, as glob expansion does not occur during variable assignment.
That said, if you just want to print the contents of the file, there's no need to use a command substitution or read
loop at all. Why not just use cat data
?
Alternatively, to store the contents of the file to a variable, just use data="$(cat data)"
(the quotes are important here), or as suggested in the comments above by gniourf_gniourf, data="$(< data)"
.