0

I have a text file. Using wc -l I can determine their are 15 lines. I am trying to create a number of variables based on the output of wc -l. So if I get 15 lines, I want 15 variables named var1 var2 and so on. Does anyone know a way to do this? The code I made seems to think i am doing a command, instead of varaible declaration. This is the following code

#!/bin/bash
VARIABLEAMT=$( wc -l myfile.txt )
NUMBER=0
while [ $NUMBER -le $VARIABLEAMT ];
do
        var${NUMBER}=0
        let NUMBER++
done




exit 0

Xenon
  • 171
  • 2
  • 9
  • 1
    Are you sure you don't just want an array instead? – jordanm Jun 26 '20 at 19:23
  • 1
    Arrays are generally preferable to putting numbers in variable names. If that'll work, use `readarray -t var – Gordon Davisson Jun 26 '20 at 19:24
  • My xy problem guess is that you want `mapfile -t somearray < myfile.txt` – jordanm Jun 26 '20 at 19:24
  • Oh okay I didn't know it would be that simple. Thank you, I will put them all in a array instead. – Xenon Jun 26 '20 at 20:07
  • The myfile contains a integer and then the date next to it. Is their a way to parse just the integers with readarray, or should I use awk, ex awk ' {print $1}'? – Xenon Jun 26 '20 at 20:10
  • consider updating the question with some sample data from `myfile.txt`; and add a description of how you want that data stored in the array – markp-fuso Jun 26 '20 at 23:24

0 Answers0