title: Regex using grep in bash gives unwanted output at the end
body: <p>I am iterating through a text file line by line in bash and printing out the word segments that match the relevant regex pattern. This process is done by accepting the text file as an argument. My code works fine but at the end, in the last line, an unwanted output gets printed. I am new to bash and I have no idea why this is happening. Shown below is my code:</p>
  
  <pre><code>#!/bin/bash
  pat='[a-z][a-zA-Z0-9_]*\.png'
  while IFS='' read -r line || [[ -n $line ]];do
     echo $line | grep -o -P $pat
  done &lt; "$1"
  </code></pre>
  
  <p>shown below is the output(the unwanted line is the highlighted one):</p>
  
  <p><a href="https://i.stack.imgur.com/adUxz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/adUxz.png" alt="enter image description here"></a></p>
  
  <p>I want to get rid of this last line of unwanted output.</p>