I am trying to implement a n x n 2D matrix using shell script. I am taking input for matrix elements using two for loop. But when I print the values after getting the elements using separate loop, It only prints last row n times. Please find below code and outputs.
#!/bin/bash
read -p "Enter the size of matrix: " n
c=`expr $n - 1`
# Get the matrix elements
for i in $(seq 0 1 $c)
do
for j in $(seq 0 1 $c)
do
read -p "enter the value of $i, $j element " arr[$i,$j]
done
done
# Print the matrix
for i in $(seq 0 1 $c)
do
for j in $(seq 0 1 $c)
do
echo -n "${arr[$i,$j]} "
done
echo
done
Output:
ubuntu@ip-172-31-6-229:~/shellscript$ ./matrix.sh
Enter the size of matrix: 3
enter the value of 0, 0 element 1
enter the value of 0, 1 element 2
enter the value of 0, 2 element 3
enter the value of 1, 0 element 4
enter the value of 1, 1 element 5
enter the value of 1, 2 element 6
enter the value of 2, 0 element 7
enter the value of 2, 1 element 8
enter the value of 2, 2 element 9
7 8 9
7 8 9
7 8 9
As you can see, While printing output, It is printing last row in all the rows. Can someone please help me with this ?
Shell: BASH
Thanks