A loop is a set of instructions in computer programming that is continuously repeated until a given condition is met. Suppose the condition is met, then the loop exits. In most cases, a specific operation is carried out, such as retrieving an item of data and modifying it. Then some condition, such as determining whether or not a counter has reached a predetermined number, is examined.
Bash is a command line interpreter, meaning it usually runs in a text window and allows the user to understand instructions to carry out various tasks. A Shell Script combines these commands in the form of a sequence contained within a file. The commands included in a Shell Script can be read by Bash, which will then carry them out.
When creating Bash scripts, you will inevitably run into circumstances where you need to read a file line by line at some point. You might, for instance, have a text file on your computer that contains data that needs to be processed by the script.
Read a file line by line in Bash
This article guide will cover the basics of reading a file line by line using the Bash shell. How would you construct a Bash script capable of processing a text file line-by-line? To begin, you will need a syntax and method to read the file one line at a time. In this tutorial, the ways used for this approach are shown.
Assume that you have a text file with the name fosslinux.txt that contains a list of the authors. The following content can be found within this file.
Abraham Enock Mercy Rutto Masai
With this info, now let us check some of the examples that can explain this subject matter:
Example 1: Using a script to read the file contents
To read the contents of a certain file, you will need to create a bash file and add the following code to it. In this case, the name of an existing file is saved in the $filename variable, and the value of the line number in that file is maintained in the $n variable. The while loop is used to read the file containing the line numbers.
#!/bin/bash filename="fosslinux.txt" n=1 while read line; do # read each line echo "Line No. $n : $line" n=$((n+1)) done < $filename
To execute the script’s instructions, run the following command:
The original content of the fosslinux.txt file can be viewed by executing the ‘cat’ command with the fosslinux.txt file.
Example 2: Read the file contents from the command line
Let’s say you don’t want to use the ‘cat’ command and instead want to read the company.txt file line by line from the command prompt. To complete the task, run the command that is listed below. The while loop will read each line from the fosslinux.txt file at each step. The contents of each line will be stored in a variable called $line, which will be printed later.
while read line; do echo $line; done < fosslinux.txt
Example 3: Use the read command and the while loop to read a file line by line
When we want to read the contents of a file one line at a time, we can use the read command. We pass the -r parameter to the read command to prevent any characters from being backslash-escaped.
#!usr/bin/env bash file="fosslinux.txt" while read -r line; do echo -e "$line\n" done <$file
In the above example, we can see that we are iterating over a file line by line and storing the contents of a single line in a variable referred to as “line.” The variable file is where the file’s name is saved, and this information can be altered to suit individual needs. You may put the script into action by typing in the following command:
Note: Change the fosslinux.sh to the actual name you gave to your script.
To read the file’s contents without escaping the backslash character, we use the read command with the -r parameter. Inside the while loop, we read the text of each line and store it in the variable line. Then, we use echo with a formatted -e parameter to display the contents of the line variable. This allows us to use special characters like n.
You can alternatively input the file by parsing it as a positional parameter. This is another option.
bash fosslinux.sh filename
You are free to choose whatever filename you want to read the contents of as the filename. You will need to adjust the script containing the variable declaration for the file.
This will utilize the name of the first parameter after the script name as the name of the file used in the script. As a result, we can instruct the script to perform dynamic changes to the file based on the provided input.
Example 4: Bypassing the filename via the command line
Make a file in the bash scripting language and add the following script. This script will get the filename from the argument given on the command line. The first case value is read by the variable $1, which will then contain the name of the file to be read. The while loop will then read the file line by line, similar to the example that came before it, and print out whatever is contained in the file if it is present at the current place.
#!/bin/bash filename=$1 while read line; do # read each line echo $line done < $filename
Execute the script that was just shown with the file fosslinux.txt set as the argument value. After deleting any unnecessary space, the output will display the contents of the fosslinux.txt file. The ‘cat’ command enables you to display the fosslinux.txt file’s contents in their original format.
bash fosslinux.sh fosslinux.txt cat fosslinux.txt
-a array - Assign the words to read to consecutive indexes in the array variable ARRAY, beginning with 0. -d delim - Rather than a newline, continue reading until the first character of DELIM is read. e - To acquire the line, use Readline. -i text - Readline should begin with the content provided in TEXT. -n nchars - Instead of waiting for a newline, return after reading NCHARS characters but respect a delimiter if fewer than NCHARS characters were read before the delimiter. -N nchars - Return only after reading precisely NCHARS characters, disregarding any delimiters, unless EOF is found or the read timer expires. -p prompt - Before reading, output the string PROMPT without a trailing newline. -r – Don't allow characters to escape through backslashes. -s - Do not repeat the input from a terminal. -t timeout - If a whole line of input is not read within TIMEOUT seconds, time out and return failure. The TMOUT variable represents the default timeout. TIMEOUT can accept fractional values. If TIMEOUT is set to zero, read returns immediately without attempting to read any data and only returns success if the input is available on the provided file descriptor. Upon exceeding the timeout, the exit status is larger than 128. -u fd - Instead of standard input, read from file descriptor FD.
As a result of the examples presented above, we could use a BASH script to read the contents files one line at a time. This article also covered the useful read commands one can use to read a file in Bash. I hope you found this article guide helpful. Please let us know your encounter in the comments section below.
We have no tolerance for comments containing violence, racism, profanity, vulgarity, doxing, or discourteous behavior. Thank you for partnering with us to maintain fruitful conversation.