Linux/UNIX: Bash Read a File Line By Line

Linux/UNIX: Bash Read a File Line By Line

To read a file line by line in a Bash shell script, you can use the while loop and the read command.

Here is an example of a script that reads a file called input.txt line by line and prints each line to the terminal:

#!/bin/bash

# Open the input file
while read line
do
    # Print the current line
    echo "$line"
done < input.txt
Source‮ual.www:‬tturi.com

To run this script, you can save it to a file (e.g., read_lines.sh) and make it executable using the chmod command:

chmod +x read_lines.sh

Then, you can run the script using the ./ command:

./read_lines.sh

This will read the file input.txt line by line and print each line to the terminal.

Overall, the while loop and read command are useful tools for reading a file line by line in a Bash shell script. They allow you to process the contents of a file in a convenient and efficient way.

Created Time:2017-10-30 10:17:48  Author:lautturi