Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read variables from file, with multiple variables per line?

Tags:

bash

shell

I'm trying to read from a file, that has multiple lines, each with 3 informations I want to assign to the variables and work with.

I figured out, how to simply display them each on the terminal, but can't figure out how to actually assign them to variables.

while read i
do
  for j in $i
  do
    echo $j
  done
done < ./test.txt

test.txt:

1 2 3
a b c

So I want to read the line in the outer loop, then assign the 3 variables and then work with them, before going to the next line.

I'm guessing I have to read the values of the lines without an inside loop, but I can't figure it out right now.

Hope someone can point me in the right direction.

like image 806
Miha Avatar asked Nov 15 '25 13:11

Miha


1 Answers

I think all you're looking for is to read multiple variables per line: the read command can assign words to variables by itself.

while read -r first second third; do
    do_stuff_with "$first"
    do_stuff_with "$second"
    do_stuff_with "$third"
done < ./test.txt
like image 199
glenn jackman Avatar answered Nov 18 '25 12:11

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!