-1

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
CompEng
  • 7,161
  • 16
  • 68
  • 122
  • 1
    Does this answer your question? [How do I pipe a file line by line into multiple read variables?](https://stackoverflow.com/questions/15442220/how-do-i-pipe-a-file-line-by-line-into-multiple-read-variables) https://stackoverflow.com/questions/49906232/how-to-read-variables-from-file-with-multiple-variables-per-line – 0stone0 Feb 02 '23 at 16:29
  • 1
    `top -n 1 -b -c | while read -r x y _ _ _ _ _ _ z _; do echo "output: $x $y $z"; done` – pjh Feb 02 '23 at 17:04

1 Answers1

2

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
Barmar
  • 741,623
  • 53
  • 500
  • 612