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
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.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$1
which 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
$1
is the first command line argument while running the script.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"
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.
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
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.
From the image you can see by directly using the code, you can also read a file.
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
n=n=$((n+1))
.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
done
is marked as the end of the for loop.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.
From the image, you can see the colors.txt file contains a white space before red and a backslash after blue.
But, when it is read using the previouswhile.sh
script, 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-r
option. On the other hand, if you want to keep the backslash and omit the white space then remove theIFS=''
from the code.
The output of the file is the same as the actual one.
#! /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$line
is 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
- How to Append to File in Bash [5 Methods]
- How to Echo Multiline to a File in Bash [3 Methods]
- How to Loop Through Files in Bash Directory [With Examples]
<< Go Back to Bash File & Directory Operations | Bash Files and Directories | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners