Skip to main content
added 90 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441
while read myVariable; do

The value of myVariable is 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.

while read myVariable; do

The value of myVariable is 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 calls 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).

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
while read myVariable; do

The value of myVariable is 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 call 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.

You can get a nonempty value from the final read if there's an incomplete line after the last newline:

$ 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.

Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

while read myVariable; do

The value of myVariable is 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 calls 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).

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