I have the following bash shell script which 90% of the time does the expected thing, but sometimes it doesn't. The problem is with the placement of the date and time. As shown, I have told the program to append the `date` at the end of `screenlog.0`. Most of the time, this happens properly. However, sometimes the date and time get appended to the *beginning* of `screenlog.0`. This is really troublesome as the format of `screenlog.0` needs to adhere to certain guidelines. Any help would be appreciated.

    #!/bin/bash
    ...
    ...

    if grep -q "dicounter_${string1}_from_${string2}" MasterFile.txt || [ -e "dicounter_${string1}_from_${string2}" ];
    then
       echo "dicounter_${string1}_from_${string2} already exists in the MasterFile or the current directory. Would you like to proceed?"
       read string3
       if [[ "${string3^}" == 'Y' ]]; then
          echo "How many times has this dicounter been retested? (e.g. 1 for first time, 2 for the second, ...)"
          read string4
          screen -S trans -L /dev/ttyACM0
          screen -S trans -X stuff 's'$(echo -ne '\015')
          sleep 8s
          screen -S trans -X quit
          date +%F~%H:%M:%S >> screenlog.0
          mv screenlog.0 dicounter_${string1}_from_${string2}~${string4}
    
       else
          exit 0
       fi
    else
      #opening screen & begin analysis
      screen -S trans -L /dev/ttyACM0
      screen -S trans -X stuff 's'$(echo -ne '\015')
      sleep 8s
      screen -S trans -X quit
      date +%F~%H:%M:%S >> screenlog.0
      mv screenlog.0 dicounter_${string1}_from_${string2}
    
    fi

Essentially, the program is to look to see if the `dicounter_..._from_...` already exists, and if not, or if the user wants another test run, proceed and connect to a transmitter and take data. At the end, a `screenlog.0` file is created, to which I want to append the date and time at the end of the file. Then rename it.