It is not the $()
command substitution that smushes the output to one line; it is the echo
of an unquoted variable. If you do echo "$SITE"
, you'll see the multi-line output.
Having said that, a common approach to reading line-by-line is the while read
construct. You could do something like this:
while read line; do
WHATEVER=$(echo "$i" | cut -d ':' -f 1 | cut -d '/' -f 3)
echo $WHATEVER
done < <(grep -w bar some/path/*/file)
Note we can use a pipe instead of the <()
process substitution on the last line, but the above is better if variables set within the while loop need to remain in scope outside of the while loop.
Of course, if this is all you need to do with the output, then the following is much more straightforward:
grep -w bar some/path/*/file | cut -d ':' -f 1 | cut -d '/' -f 3