 <p>An efficient way to print nth line from a file (<strong>especially suited for large files</strong>):</p> <pre><code>sed '2q;d' file </code></pre> <p>This sed command quits just after printing 2nd line rather than reading file till the end.</p> <p>To store this in a variable:</p> <pre><code>line=$(sed '2q;d' file) </code></pre> <p>OR using a variable for line #:</p> <pre><code>n=2 line=$(sed $n'q;d' file) </code></pre> <p><strong>UPDATE:</strong> </p> <p><strong>Java Code:</strong></p> <pre><code>try { ProcessBuilder pb = new ProcessBuilder("/bin/bash", "/full/path/of/myScript.sh" ); Process pr = pb.start(); InputStreamReader isr = new InputStreamReader(pr.getInputStream()); BufferedReader br = new BufferedReader(isr); String line; while((line = br.readLine()) != null) System.out.println(line); int exitVal = pr.waitFor(); System.out.println("exitVal: " + exitVal); } catch(Exception e) { e.printStackTrace(); } </code></pre> <p><strong>Shell script:</strong></p> <pre><code>f=$(dirname $0)/edit.txt read -r i &lt; "$f" echo "session is: $i" echo -n "file path is: " sed '2q;d' "$f" </code></pre>