I have a while loop that allows setting variables when written one way but does not allow it when written another way. Why is that? This prints var as 1234 and does not print var2
#!/bin/bash
var=1234
while var=9876 var2=765 read line
do
echo $var $var2
echo $line
done <datafile
echo out of loop $var
This prints var as 9876 and also prints var2
#!/bin/bash
var=1234
while
var=9876
var2=765
read line
do
echo $var $var2
echo $line
done <datafile
echo out of loop $var
read
to have a local IFS value, thenIFS='?' read
is the only way. Note that all of the commands betweenwhile
anddo
are called consecutively each time it loops. Thewhile
only exits when the last command exits with non-zero. Construct-wiseread
is independant of the 'do--doneblock. You could have your do-stuff in a command placed between
readand
doand have nothing in
do-while`. However, to leave the while loop, the onus is on your last command to exit with a non-zero value at the appropriate time. – Peter.O Nov 07 '11 at 12:47