This is my script:#!/bin/bashE=0;j=0for i in A B Cdo  Erray=$(sed -n '/Local net energy/p' log | awk '{print $7}')  A[j]=j  echo $i = $Erray kJ/mol >> Energy  let j=j+1  echo $j $i $Erray ${A[j]}donecat EnergyThe output is: 1 A 2.692036739192E+032 B 2.679751695663E+033 C 4.732453844269E+03A = 2.692036739192E+03 kJ/molB = 2.679751695663E+03 kJ/molC = 4.732453844269E+03 kJ/molIt should not be that. The order of the output is sorted and the ${A[j]} doesn't appear.Edit Now I know why the output is sorted. I forgot to add >> Energy after the second echo. The array problem still remains. My input data is large, I only extract lines which are relevant.print energy 2 - 1 end  Local net energy (PE 0) = 2.692036739192E+03 kJ/mol  Global net ELEC energy = 2.692036739192E+03 kJ/mol
For output weird
shell script
In your script, j is monotonically incrementing - it starts from 0 and counts up by one per iteration of the loop.  You assign a value to A[j] in your loop, then you increment j, then you try to print out A[j] - but now that j has been incremented, that index in A hasn't been set yet, so there's nothing to print (the variable expansion is empty).  If you were printing ${A[j-1]} instead you should see output.