Search code examples
bashsh

How to use multiple variable in for loop in sh?


I want to use multiple variable in for loop at once in sh.

I have a query like this:

top -n 1 -b -c| awk -vOFS="\t" '{print $1,$2,$9}'

I know i use for loop in bash like this:

for i in {2..10}
do
    echo "output: $i"
done

what i want to try is:

for x y z in $(top -n 1 -b -c| awk -vOFS="\t" {print $1,$2,$9}')
do
    echo "output: $x $y $z"
done

Solution

  • Pipe to a while read loop:

    top -n 1 -b -c| awk -vOFS="\t" '{print $1,$2,$9}' | while IFS=$'\t' read -r x y z
    do
        echo "output: $x $y $z"
    done