Title: convert a line into array and print elements of array as variables in bash
Tags: arrays;bash
Question: I am new in linux and I try reading a text file line by line. The lines are numbers. I want to add each line in an array and consider each number as a variable. My trial is as below:
Example of txt file:

```1976   1   0  0.00    0.    68.    37.     0.   105.  0.14 0.02    4.3    1.1    2.2
```

What I need:
Putting each number in a variable. for example ```a = 1976``` and ```b = 1``` etc...
My code:

```IFS=$'\n'
for next in `cat $filename`
do

line=$next

echo ${line[0]}
done
```

Result:

```1976   1   0  0.00    0.    68.    37.     0.   105.  0.14 0.02    4.3    1.1    2.2
```

Comment: I am searching for a comand using awk to do this

Comment: I found this command which results in the first number in the array 1976 but i do not know how to loop over the rest of the array
echo $array | sed 's/^.* \(".*"$\)/\1/'

Comment: I tried this with changing the number in {print $0} to get the second and third field but it prints 1976 each time :
while read -r line; do
  array=( $line )
  echo $array | awk '{print $0}'
done < read1.out

Comment: I suggest to use `awk` for this.

Comment: Is it ok if it would be array?

Here is the accepted answer: It's quite easy to store each value in an array. Here is an example:

```while read -r -a line
do
 echo "${line[0]}"
 echo "${line[1]}"
 echo "${line[2]}"
done < $filename
```

-a line splits the input-line into words (white space seperated by default) and store the results in ```line``` array.

A snippet from read man:

```
  -a     Each name is an indexed array variable (see Arrays above).
```

You man not need the '-r' option. It basically make read to treat \ as nothing special in the input. 

Comment for this answer: thank you sir, the provided script works with removing the dollar sign from the file name
Comment for this answer: Sir, 
  Sorry I am still new. The fields are well extracted by your script but i do not know how to save them in variables for instance a=echo "${line[0]}" does not work
Comment for this answer: Omar's code in the question suggested me that's what he's trying to achieve actually.
Comment for this answer: You can assign like so: `a=${line[0]}` , without echo. Still I'm not sure what you're trying to achieve and how helpful is that for you. That way you assign variables in a loop. You can still access them after the loop is executed, but if you do something like `a=${line[0]}` then `a` would contain a value for the last line read.

Here is another answer: ```# Add each line to Array
readarray -t aa < $filename
# Put each line into variables using Here String
for l in "${aa[@]}"; do
  read a b c <<< $l;     # Example using 3 variables, could be as many as on line
  # Do whatever has to be done with a, b, c, etc
done
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:01/03/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<arrays><bash>', 'Title': 'convert a line into array and print elements of array as variables in bash', 'ViewCount': 553, 'id': 34575058, 'user_AboutMe': None, 'user_AccountId': 7563457.0, 'user_DisplayName': 'Omar Ali', 'user_Reputation': 85.0, 'user_Views': 18.0, 'user_id': 5740006.0}}