I tried to loop a command which returns a multi-line result in a bash script. The problem was that the quoted command returned ONE line instead of each line separately. So I ended with one line consisted of multi-line result while I was expecting that the quotes would preserve the spacing. Without quotes I got each "word" in a new line while I was expecting everything in one line (without quotes should replace multiple blanks, tabs and newlines with a single space). Here is the "faulty" script:
#for line in `ls -l` #for line in $(ls -l) for line in "$(ls -l)" do echo $line"XXX" done
Not quite sure if this is due to OS X (or BSD) environment but I got around it by piping the command to the while loop:
(ls -l) | while read line do echo $line"xxx" done
while read line; do echo $line"xxx" done < <(ls -l)
TrackbacksTrackback specific URI for this entry
This link is not meant to be clicked. It contains the trackback URI for this entry. You can use this URI to send ping- & trackbacks from your own blog to this entry. To copy the link, right click and select "Copy Shortcut" in Internet Explorer or "Copy Link Location" in Mozilla.