0

Why does my while loop skip the last element? I see similar discussion for cases where trailing newlines are missing (e.g. bash while loop drops last line of text file) but it doesn't seem this applies to read -d.

$ IP=1:2:3:4:5:6:7:8
$ while read -d : -r part; do echo $part; done <<< $IP
1
2
3
4
5
6
7

I'm using GNU bash 4.2.

markdrayton
  • 485
  • 6
  • 11
  • 1
    You need a trailing `:` for the final `read` command to report success. The duplicate questions' answers really are applicable; appending `|| [[ $part ]]` as they recommend will fix your problem here. – Charles Duffy Mar 01 '18 at 22:45
  • 1
    It's exactly the same reason as that other question. You've simply replaced the default newline with `:`. – Barmar Mar 01 '18 at 22:46
  • Thanks. I was hoping it was somehow less clunky. Feel free to copypasta to answers and I'll accept. – markdrayton Mar 01 '18 at 22:49

0 Answers0