The for
loop is not designed to loop over lines. The input is split using the characters in the $IFS
variable as separators. Usually $IFS
contains a space, a tab, and a newline. That means the for
loop will loop over the "words", not over the lines. One way to loop over lines is to change the value of $IFS
to only newline. If you do this then you have to save the old value of $IFS
and restore that after the loop. A better way is to use the a while
loop in combination with read
.
Change $IFS
to only newline:
OLDIFS=$IFS
IFS=$'\n'
for line in $(ioscan -m dsf)
do
echo $line
done
IFS=$OLDIFS
or use a subshell to contain the change to $IFS
:
(
# changes to variables in the subshell stay in the subshell
IFS=$'\n'
for line in $(ioscan -m dsf)
do
echo $line
done
)
# $IFS is not changed outside of the subshell
Better: use a while
loop with read
:
ioscan -m dsf | while read line
do
echo $line
done
See also: