How to read a file line by line in Bash.

<pre lang="bash">
#!/bin/bash
file=test.txt
while IFS= read -r line; do
  echo $line
done 
<p>Contents of test.txt.</p>
<pre lang="bash">
one
two
three

<p>Results when running test.sh.</p>
<pre lang="bash">
$ bash test.sh
one
two
three

<p>To read multiple strings line by line.</p>
<pre lang="bash">
#!/bin/bash
file=test.txt
while read -r a b; do
  echo $b
done 
<p>Modified contents of test.txt.</p>
<pre lang="bash">
one 1
two 2
three 3

<p>New results.</p>
<pre lang="bash">
$ bash test.sh
1
2
3