0

How to read variables line after line

 PROMOTEBUILDS=3.1.161,1.0.132
    echo cat ${PROMOTEBUILDS} | while read -r line; do
    egrep $line /home/rahul-optit/packages
    done

3 Answers 3

3

@Jesse_b's answer is the way to do it.

If you however have no influence on the input, you can use IFS=, and read to split the string and make it an array.

printf '%s' "$PROMOTEBUILDS" | IFS=, read -a promotebuilds
for i in "${promotebuilds[@]}"; do
  egrep "$i" /home/rahul-optit/packages
done
2

Since you're using bash you should use an array instead of a variable:

promotebuilds=( 3.1.161 1.0.132 )
for i in "${promotebuilds[@]}"; do
  egrep "$i" /home/rahul-optit/packages
done
0

Maybe I do not understand the question. For one thing, your variable appears to have only one line.

All this time I've been reading lines from a variable the same way you would from a regular file except I enclose it in quotation marks and use '<<<' instead of '<' to feed it to the while loop.

VARIABLE=$(cat somefilewithmultiplelines)
while read i ; do echo "$i" ; done <<< "$VARIABLE"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .