Skip to main content
3 of 3
added 107 characters in body

Looping through two files with a for loop and evaluating the indices

I am learning bash shell scripting and am looking for pure bash ways rather than awk, etc.

I am trying to use a for loop to go through the contents of two files that are submitted as arguments in the terminal so that I can write expressions from the information.

The file contents are separated with tabs. The files have no extensions. Here is an example of the type of the file information I want to evaluate:

$cat file1
1     2     3
40    50    60

$cat file2
10     20     30
40     50     60

Here is the code that I have written:

read line1 < "file1"
read line2 < "file2"

difference=0

#I can see the contents of file1 with the below code and by changing the code
#slightly I can see the contents of file2 as well using the following code:

for index1 in $line1
do
echo "The contents of index1 are: $index1"
done

#But I am trying to do something like this which isn't working:
for index1 in $line1, index2 in $line2
do
difference=$(expr $index1 - $index2)
echo $difference
done