Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading