Unlike my default shell (zsh), bash has a wonderful feature where it doesn’t keep variables that are set at the other end of a pipe, so for example:
cat foo | while read bar; do
Yields an empty line. I’ve been stung once or twice on this as I prototype the code initially in an interactive shell, which doesn’t exhibit the issue.
The simplest solution is to use a named pipe.
cat foo >/tmp/foo$$&
while read bar; do
This gives the last line of the file in the i variable.