while read myVariable; doThe value of
myVariableis lost when leaving the loop.
 No, myVariable has the value it got from the last read. The script reads from the file, until it gets to the position after the last newline. After that, the final read callscall gets nothing from the file, sets myVariable to the empty string accordingly, and exits with a false value since it didn't see the delimiter (newline). Then the loop ends.
 IfYou can get a nonempty value from the final read if there's an incomplete line after the last newline, then read can set the variable to a nonempty value, and still return false.:
$ printf 'abc\ndef\nxxx' | 
    { while read a; do echo "got: $a"; done; echo "end: $a"; } 
got: abc
got: def
end: xxx
 Or use while read a || [ "$a" ]; do ... to handle the final line fragment within the loop body.
 
                