 <p>The best way to do this would be to use <code>grep</code> to get the lines, and populate an array with the result using newline as the internal field separator:</p> <pre><code>#!/bin/bash # get just the desired lines results=$(grep "mypattern" mysourcefile.txt) # change the internal field separator to be a newline IFS=$'/n' # populate an array from the result lines lines=($results) # return the third result echo "${lines[2]}" </code></pre> <p>You could build a loop to iterate through the results of the array, but a more traditional and simple solution would just be to use bash's iteration:</p> <pre><code>for line in $lines; do echo "$line" done </code></pre>