How to Read Files in Bash [4 Methods]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Reading a file is a process that involves the access of read and manipulation of its content. It is important to read files in bash for automation, scripting, error handling and validation, and configuration files. There are various ways in which a file can be read in bash depending on specific criteria of files. Let’s see the methods to read files in bash.

4 Methods to Read Files in Bash

In bash scripting, one file can be read with the cat command, with $, using a loop and directly from the command line. In this part, I will give an overview of the four methods to check how one can read a file.

Method 1: Read the File with ‘cat’ Command

To read a file in bash, you can use the simple cat command. It is a short but very powerful method to read small to moderate-sized files. You can use the following code in the nano text editor:

#! /bin/bash 
filename="weekdays"
 if [ -e "$filename" ]; then 
    content=$(cat "$filename") 
    echo "$content" 
else 
    echo "File not found: $filename" 
fi

EXPLANATION
Here filename should be specified in the file that you want to read. I have used a file named ‘weekdays’. The if statement with the “-e” option is used to check whether the file exists or not. The cat command reads the line into the content file which is substituted by “$”. The echo command is used to print the content of the file.

Read file in bash script with cat command

Use the command as shown in the image to read the file. I have named the script in File2.sh, you can name the bash script according to your preference.

You can also simply use the following code:

#! /bin/bash
filename="colors.txt" 
content=$(cat "$filename") 
echo "$content"

It should be in mind that, in this case, the bash script and the file are in the same directory. If your file and script are in different directories then replace the filename in this code with the file’s path like /path/filename. For example, if the colors.txt file is in the Document folder then the path will be /home/oishi/Document/colors.txt.

Read File From Command Line

To read a file from the command line, replace the filename with$1which refers to the first command line argument passed to the script. When you execute the script, providing the filename as an argument, the filename is assigned to the argument$1. Use the following code to read the file from the command line.

#! /bin/bash
filename=$1
if [ -e "$filename" ]; then
   content=$(cat "$filename")
   echo "$content"
else 
    echo "File not found: $filename"
fi
EXPLANATION
$1is the first command line argument while running the script.

Read file from command line

The script will show the contents of the following file.

You can use this process in all the methods by replacing the filename with$1.

Method 2: Read File to Variable in Bash

The ‘$’ symbol is used to access the value of a variable. However, when it comes to reading files in Bash, it’s used to reference variables that might contain file paths or filenames. For instance, $(<"$filename") is used to read the contents of the file whose name is stored in the filename variable. The following script shows an example of how to use it:

#! /bin/bash 
Readfile=$(<weekdays) 
echo "$Readfile"
EXPLANATION

Here the < is the input redirection which instructs the shell to read the content of the file called weekdays. The command substitution $, took the output of <weekdays.

Read file in bash using $In the output, the file contents are displayed which is the result of the echo command.

Method 3: Read File Line by Line Using Loop

For reading a large file, using a loop is efficient because it is very effective for reading each line of a large file. Let’s see how to read a file using a while and a for loop.

Using While Loop to Read file

Use the following script to read line by line content of a file using the while loop:

#! /bin/bash
while read line; do
echo $line
done < weekdays
EXPLANATION

The read command is used to read the file and its input is the < weekdays. Here, weekdays is the filename that the script read. Inside the loop, the echo command is used to print each line of weekdays.

Read file with while loop

One can also use this script code in the command line then he also can be able to read the file just like the image I have shown below:
Read file from command lineFrom the image you can see by directly using the code, you can also read a file.
Read Each line with the Line Number:
If you want to read a file with numbering the lines then you can copy the following code which I named as File1.sh.
#! /bin/bash 
filename='weekdays'
n=1 
while read line; do
echo "Line No. $n : $line" 
n=$((n+1)) 
done < $filename

EXPLANATION
The number of lines initiates with 1 at the first of the code. In the loop, it iterates over the line with an arithmetic operation which is n=n=$((n+1)).
Read file line by lineFrom the output, you can see the number of lines has been shown.
It not only shows the count of all number of lines in your file but also lets you read a specific line.

Using For Loop to Read File

You can also read files using a for loop. Run the following code for reading large files and save this into a specific file name:

#! /bin/bash
Readfile="weekdays"
for line in $(cat "$Readfile"); do
echo "$line"
done

EXPLANATION
The for loop iterates over each line until the line of the input file which is in the substitution command. The last line done is marked as the end of the for loop.
Using for loop to read a file

Method 4: Read Files with Whitespace and Backslash (\)

If one file contains white space ( ) or other special characters like a backslash (\) then it is omitted while reading the file from the script.
Contain back slash and white space From the image, you can see the colors.txt file contains a white space before red and a backslash after blue.
Read file using previous while file

But, when it is read using the previouswhile.shscript, the space and backslash have been omitted, and the two lines before and after the backslash will be connected. If you want to keep the special character and space the same then use the following code as the previous one with a statement''on the top of the code to omit the input field separator or IFS like the below script:

#! /bin/bash 
IFS='' 
while read -r line; do 
echo $line 
done < colors.txt

If you want to keep the space and omit the backslash then you remove the-roption. On the other hand, if you want to keep the backslash and omit the white space then remove theIFS='' from the code.
Reading the original file The output of the file is the same as the actual one.

It is the best practice to use this structured code:
#! /bin/bash
while IFS= read -r line; do 
echo "$line" 
done < colors.txt

To avoid space and handle special characters in a better way try to use IFS and the “-r” option.
The IFS= parameter is an empty string that prevents whitespaces from being cut. It should be in mind that if the variable$lineis not quoted then it will show an error.

Conclusion

In this article, four methods are shown to read a file. Using a loop for reading a file is comparatively the best method in my opinion because when the file is large then you can easily read as it iterates over each line. So, you can try each method and choose the best method. It completely depends on file criteria and user preference.

People Also Ask

Can I read a file without using the bash script?

Yes, with the cat, less, more, tail, and head you can read files but it is sometimes difficult to handle large files with the cat command. For that, the tail can be used to read the end part of a large file.

How does a read command work?

The read command processes the lines of the file and assigns each line to a variable. The loop command exits once the processing is complete. The syntax of the read command is read [option] [variable]. The options of the read command are -r, -n, -t, and -p. It takes the value provided by the user and stores it in the name variable.

Why does one use %s\n before the variable in reading a file?

With echo or printf commands, %s can be used to treat the input as a string and \n add a new line character after each line.

Can I show file contents without using echo?

Yes, you can use the printf instead of echo. It is an external command and is available in most Unix systems. cat command is also a very powerful command to display and concatenate the content of the file. But printf shows more flexibility than the cat command.

Related Articles


<< Go Back to Bash File & Directory Operations | Bash Files and Directories | Bash Scripting Tutorial

5/5 - (1 vote)
Afia Zahin Oishi

Assalamualaikum, I am Afia Zahin, completed my graduation in Biomedical Engineering from Bangladesh University of Engineering and Technology, currently working as a Linux Content Developer Executive at SOFTEKO. A high achieving professional with a strong work ethic and able to work in a team in order to consistently achieve my goal and build my skillset. Able to handle difficult problems with patience and swift decision-making. Read Full Bio

Leave a Comment