Press "Enter" to skip to content

Linux/Unix: Read a File Line by Line Using Bash Shell Script

Here I am giving an example of a Linux program to read a file line by line using the bash shell script.

Read a File Line by Line Using Shell Script

The following shell script will prompt the user to enter a file name and then it will read the file content line by line using the while loop and will assign each line into a variable and then it will print on the screen.

#!/bin/bash
echo "Enter the name of file:"
read filename
exec<$filename
while read var_line
do
echo $var_line
done

Output

$ ./readfile.sh
Enter the name of file:
file.txt
this is first line.
this is second line.
this is third line.

See also: