getimad
getimad

Reputation: 1

Why the while loop doesn't read the final input?

I am trying to read all inputs using while loop and print them again, but for some reason doesn't read the final input.. What's going on?

#!/usr/bin/env bash

while read -r num; do
    echo $num
done
Input (stdin):
4
1
2
9
8
Output (stdout):
4
1
2
9
# Last input is missing :(
Expected Output:
4
1
2
9
8

Upvotes: 0

Views: 68

Answers (1)

Geno Chen
Geno Chen

Reputation: 5214

You may forget to add a new line at the end of input.

4
1
2
9
8 # A new line expected, after the character "8".

Or we say, 4\n1\n2\n9\n8\n.

Otherwise, the last line won't be "enter"ed into the script / program.

Upvotes: 1

Related Questions