I was trying to read a file line by line(each line is a hostname), do some processing like this :
while read -r line
do
if ping -c1 $line
echo $line is running
fi
done <file.txt
I found that the out put is like this:
is running
is running
...
As if the variable $line is empty.
Thanks to all who commented this question. I think I now understand what happened.
So the file has CRLF line ending. And the length of a line is exactly the same as is running.
Because of the CR character at the end of $line, the content of $line gets completely overwritten by is running. If only the length of a line is longer, I would have asked the question differently.
$linestart with a dash (-)? What if you useprintf '%s is ok' "$line"?file.txthave Window-style (CRLF) line endings?echo $lineafterfi, aka outside of the if block, it would work perfectly. The text file contains a list of hostnames, lines don't start with dash. I'm sure the content can be read ok asechooutside of the if works.