{"id":35085,"date":"2024-01-25T10:25:11","date_gmt":"2024-01-25T10:25:11","guid":{"rendered":"https:\/\/linuxsimply.com\/?p=35085"},"modified":"2024-03-31T10:15:13","modified_gmt":"2024-03-31T10:15:13","slug":"bash-while-loop-read-file","status":"publish","type":"post","link":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/","title":{"rendered":"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases]"},"content":{"rendered":"<p style=\"text-align: justify;\">Reading a file line by line is a must-have skill that any developer needs to learn to master <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/introduction\/what-is-bash-scripting\/\" target=\"_blank\" rel=\"noopener\">bash scripting<\/a><\/u><\/strong>. One of the easiest methods to read file content is the use of a <strong>read<\/strong> command in conjunction with a <strong>while loop<\/strong> that loops through a file line by line and facilitates data manipulation and processing. The while loop with the read command can be used in various ways for reading file information. In this article, I will walk you through the 8 cases to read a file line with the while loop in Bash. Stay tuned.<\/p>\n<div class=\"su-button-center\"><a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/Practice_files_bash-while-loop-read-file.zip\" class=\"su-button su-button-style-default\" style=\"color:#000000;background-color:#E8F9FF;border-color:#bac8cc;border-radius:56px\" target=\"_blank\" rel=\"noopener noreferrer\"><span style=\"color:#000000;padding:8px 26px;font-size:20px;line-height:30px;border-color:#effbff;border-radius:56px;text-shadow:0px 0px 0px #000000\"><i class=\"sui sui-download\" style=\"font-size:20px;color:#1AA1D6\"><\/i> Practice Files to Read File Line Using Bash \u201cwhile\u201d Loop<\/span><\/a><\/div>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#8_Cases_to_Read_File_Line_Using_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\" >8 Cases to Read File Line Using \u201cwhile\u201d Loop in Bash<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#1_Using_%E2%80%9Cread%E2%80%9D_Command_Alone\" >1. Using \u201cread\u201d Command Alone<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#2_Using_%E2%80%9Ccat%E2%80%9D_Command\" >2. Using \u201ccat\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#3_Using_IFS_Internal_Field_Separator\" >3. Using IFS (Internal Field Separator)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#4_Using_Command-line_Argument\" >4. Using Command-line Argument<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#5_Using_%E2%80%9Cawk%E2%80%9D_Command\" >5. Using \u201cawk\u201d Command<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#6_Using_here_String\" >6. Using here String<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#7_Using_File_Descriptor\" >7. Using File Descriptor<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#8_Using_Process_Substitution\" >8. Using Process Substitution<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#How_to_Read_File_Line_in_Reverse_Order\" >How to Read File Line in Reverse Order?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#Common_Issues_in_Bash_File_Reading_With_%E2%80%9Cwhile%E2%80%9D_Loop\" >Common Issues in Bash File Reading With \u201cwhile\u201d Loop<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#Dealing_With_Empty_Lines\" >Dealing With Empty Lines<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#Managing_Special_Characters\" >Managing Special Characters<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#Practice_Tasks_on_How_to_Read_File_Line_Using_Bash_%E2%80%9Cwhile%E2%80%9D_Loop\" >Practice Tasks on How to Read File Line Using Bash \u201cwhile\u201d Loop<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#People_Also_Ask\" >People Also Ask<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#How_to_skip_empty_lines_or_lines_starting_with_specific_characters\" >How to skip empty lines or lines starting with specific characters?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#How_does_the_while_loop_read_a_file_line_by_line_in_Bash\" >How does the while loop read a file line by line in Bash?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#How_to_exit_the_loop_early_based_on_a_condition\" >How to exit the loop early based on a condition?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#What_is_read_command_in_Bash\" >What is read command in Bash?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"8_Cases_to_Read_File_Line_Using_%E2%80%9Cwhile%E2%80%9D_Loop_in_Bash\"><\/span>8 Cases to Read File Line Using \u201cwhile\u201d Loop in Bash<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">A <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/\" target=\"_blank\" rel=\"noopener\">while loop<\/a><\/u><\/strong> offers versatile features, so it can be used in combination with the read command in 8 ways to read a file line. Here. I will explain how the read, <strong>cat<\/strong>, <strong>awk<\/strong>, <strong>grep<\/strong>, etc. commands are used for reading file content. Additionally, to read file lines using command-line argument, IFS variable, and process substitution will also be addressed.<\/p>\n<p style=\"text-align: justify;\">Download this <a href=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/file.txt.zip\" target=\"_blank\" rel=\"noopener\"><strong>file.txt<\/strong><\/a> to work with the following methods. Here\u2019s the content of the file:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-35156\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/file-content.png\" alt=\"contents of file.txt\" width=\"825\" height=\"142\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/file-content.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/file-content-300x52.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/file-content-768x132.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Using_%E2%80%9Cread%E2%80%9D_Command_Alone\"><\/span>1. Using \u201cread\u201d Command Alone<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To read a file line in bash, use only the <strong>read<\/strong> command within the while loop. The read command can read an entire file line by line through a while loop. Follow the script below to see how the while loop works with the <code class=\"\" data-line=\"\">read<\/code> command for reading file lines:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;file.txt&quot;\nwhile read line; do\necho $line\ndone &lt; &quot;$filename&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>Here, <code class=\"\" data-line=\"\">filename=&quot;file.txt&quot;<\/code> assigns the <strong>file.txt<\/strong> to the variable <strong>filename<\/strong>. Then the <code class=\"\" data-line=\"\">while read line; do<\/code> starts the while loop and continues to read file line by line and <strong>echoes <\/strong>each line (read by the <code class=\"\" data-line=\"\">read<\/code> command) to the terminal with the <code class=\"\" data-line=\"\">echo $line<\/code>. Finally, the <code class=\"\" data-line=\"\">done &lt; $filename<\/code> uses <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/redirection-and-piping\/redirection\/input-redirection\/\" target=\"_blank\" rel=\"noopener\">input redirection<\/a><\/u><\/strong> <code class=\"\" data-line=\"\">(&lt;)<\/code> to redirect the input from the file specified by the &#8220;filename&#8221; variable.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-35089\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1.reading-file-with-read-command-using-while-loop.png\" alt=\"reading file line with read command using while loop\" width=\"825\" height=\"242\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1.reading-file-with-read-command-using-while-loop.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1.reading-file-with-read-command-using-while-loop-300x88.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/1.reading-file-with-read-command-using-while-loop-768x225.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">This picture shows that the script prints each line of <strong>file.txt<\/strong>. However, it does not show the backslash sign<strong> \\<\/strong> from the file. <a id=\"method_1\"><\/a>To preserve the backslashes, use the <code class=\"\" data-line=\"\">-r<\/code> option with the <code class=\"\" data-line=\"\">read<\/code> command. Here\u2019s how:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;file.txt&quot;\nwhile read -r line; do\necho $line\ndone &lt; &quot;$filename&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <code class=\"\" data-line=\"\">while read<\/code> command with the <code class=\"\" data-line=\"\">-r<\/code> option reads the file line from \u201cfile.txt\u201d including backslashes and prints the line that is read by the <code class=\"\" data-line=\"\">read<\/code> command in the terminal using the <strong><u><a href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/input-output\/output\/echo-command\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a><\/u><\/strong>.<\/p>\n<\/div><\/div>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-35090\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2.reading-file-including-backslashes-with-read-command-using-while-loop.png\" alt=\"reading file line including backslashes with read command using while loop\" width=\"825\" height=\"204\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2.reading-file-including-backslashes-with-read-command-using-while-loop.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2.reading-file-including-backslashes-with-read-command-using-while-loop-300x74.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/2.reading-file-including-backslashes-with-read-command-using-while-loop-768x190.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Now, you can see that the file is read including <strong>backslashes<\/strong> by the <strong>read<\/strong> command in a while loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2_Using_%E2%80%9Ccat%E2%80%9D_Command\"><\/span>2. Using \u201ccat\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In this example, the <strong><u><a href=\"https:\/\/linuxsimply.com\/cat-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">cat command<\/a><\/u><\/strong> is piped with the <code class=\"\" data-line=\"\">while read -r line<\/code> to read line from the <strong>file.txt<\/strong>. The piping operator <code class=\"\" data-line=\"\">|<\/code> is used to take the output of the <strong>cat<\/strong> command as input of the while loop. Copy the script to read a file line using the <code class=\"\" data-line=\"\">cat<\/code> command:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;file.txt&quot;\ncat &quot;$filename&quot; | while read -r line\ndo\necho $line\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <code class=\"\" data-line=\"\">cat<\/code> command concatenates and displays the content of the specified file. Then the output is piped to the while loop as input. Lastly, the <code class=\"\" data-line=\"\">while loop<\/code> with the <code class=\"\" data-line=\"\">read -r<\/code> command reads each line of the <strong>file.txt<\/strong> and prints the read line in the terminal using the <code class=\"\" data-line=\"\">echo<\/code> command.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35091\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3.reading-file-with-cat-command-using-while-loop.png\" alt=\"reading file with cat command using while loop\" width=\"825\" height=\"204\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3.reading-file-with-cat-command-using-while-loop.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3.reading-file-with-cat-command-using-while-loop-300x74.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/3.reading-file-with-cat-command-using-while-loop-768x190.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">As you can see, the script reads the file content and prints it in the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3_Using_IFS_Internal_Field_Separator\"><\/span>3. Using IFS (Internal Field Separator)<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Within a while loop, the <strong>IFS<\/strong> (Internal Field Separator) can be incorporated with the <code class=\"\" data-line=\"\">read<\/code> command to read a file line by line. The value of IFS determines which characters are treated as field separators. Now, take a look at the following bash script to use the <code class=\"\" data-line=\"\">IFS<\/code> with a <code class=\"\" data-line=\"\">while<\/code> loop to read file content:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;file.txt&quot;\nwhile IFS= read -r line\ndo\necho $line\ndone &lt; &quot;$filename&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p><code class=\"\" data-line=\"\">while IFS= read -r line<\/code> reads line by line from the <strong>file.txt<\/strong>. Here, the <code class=\"\" data-line=\"\">IFS<\/code> sets to nothing to preserve the leading and trailing whitespaces. After that, the <code class=\"\" data-line=\"\">echo<\/code> command prints each read line by the while loop.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35093\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5.reading-file-using-IFS.png\" alt=\"reading file using IFS\" width=\"825\" height=\"202\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5.reading-file-using-IFS.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5.reading-file-using-IFS-300x73.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/5.reading-file-using-IFS-768x188.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">You can see in the image that all the lines of the &#8220;file.txt&#8221; are printed on the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Using_Command-line_Argument\"><\/span>4. Using Command-line Argument<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In this example, to read the file content, the <strong>filename<\/strong> will be taken as a command-line argument. Here, the <code class=\"\" data-line=\"\">$1<\/code> is a positional parameter indicating the first argument passed to the script. To know how to use it for reading file lines, check the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=$1\nwhile read -r line; do\necho $line\ndone &lt; $filename<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>Before starting the while loop, the <code class=\"\" data-line=\"\">$1<\/code> passed the <strong>file.txt<\/strong> from the command line to the variable <strong>filename<\/strong>. Then the <code class=\"\" data-line=\"\">while read -r line<\/code> continues to read the file content including the backslash character due to the <code class=\"\" data-line=\"\">-r<\/code> option with the <code class=\"\" data-line=\"\">read<\/code> command. After reading the entire file, the script outputs the content to the console by the <code class=\"\" data-line=\"\">echo<\/code> command.<\/p>\n<\/div><\/div>\n<p style=\"text-align: justify;\">Run the script with this command to get the following output:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/argument.sh file.txt<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35094\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6.reading-file-using-command-line-argument.png\" alt=\"reading file line using command-line argument with while loop\" width=\"825\" height=\"203\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6.reading-file-using-command-line-argument.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6.reading-file-using-command-line-argument-300x74.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/6.reading-file-using-command-line-argument-768x189.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p>As you can see, every line of the <strong>file.txt<\/strong> is shown on the terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"5_Using_%E2%80%9Cawk%E2%80%9D_Command\"><\/span>5. Using \u201cawk\u201d Command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A powerful text-processing tool in Linux and other Unix-like operating systems is the <strong><u><a href=\"https:\/\/linuxsimply.com\/awk-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">awk command<\/a><\/u><\/strong>. In order to read the file line by line, the while loop is piped with the <code class=\"\" data-line=\"\">awk<\/code> command here:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;file.txt&quot;\nawk &#039;{print}&#039; &quot;$filename&quot; | while read -r line\ndo\necho $line\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The script uses the <code class=\"\" data-line=\"\">awk<\/code> command to print each line of the <strong>file.txt<\/strong> specified by the variable <strong>filename<\/strong>. Then the output of this command is passed to the <code class=\"\" data-line=\"\">while<\/code> loop as input through the pipe operator. <code class=\"\" data-line=\"\">while read -r line<\/code> reads file lines from the standard input (the output of the <code class=\"\" data-line=\"\">awk<\/code> command) and prints them in the terminal using <code class=\"\" data-line=\"\">echo $line<\/code>.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35095\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7.reading-file-using-awk-command.png\" alt=\"reading file using awk command\" width=\"825\" height=\"202\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7.reading-file-using-awk-command.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7.reading-file-using-awk-command-300x73.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/7.reading-file-using-awk-command-768x188.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">The image illustrates each line of the &#8220;file.txt&#8221; read by the while loop.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"6_Using_here_String\"><\/span>6. Using here String<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The <strong>here string<\/strong> in bash passes a string as the input to a command using the <code class=\"\" data-line=\"\">&lt;&lt;&lt;<\/code> operator. This example shows how to use this <code class=\"\" data-line=\"\">here string<\/code> operator to read a file line by line. To achieve this, copy the script:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;file.txt&quot;\nwhile read -r line\ndo\necho &quot;$line&quot;\ndone &lt;&lt;&lt; &quot;$(cat &quot;$filename&quot;)&quot;<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>After assigning the <strong>file.txt<\/strong> to the variable <strong>filename<\/strong>, <code class=\"\" data-line=\"\">while read -r line<\/code> continues to read the file line one by one. The <code class=\"\" data-line=\"\">echo<\/code> command displays each line to the console. <code class=\"\" data-line=\"\">done &lt;&lt;&lt; &quot;$(cat &quot;$filename&quot;)&quot;<\/code> uses here string operator <code class=\"\" data-line=\"\">&lt;&lt;&lt;<\/code> to provide the file content as input to the while loop. The <code class=\"\" data-line=\"\">cat $filename<\/code> reads the contents of the file and the output is captured by the command substitution \u201c$(&#8230;)\u201d.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35096\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8.reading-file-using-here-string.png\" alt=\"reading file line using here string in a while loop\" width=\"825\" height=\"204\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8.reading-file-using-here-string.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8.reading-file-using-here-string-300x74.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/8.reading-file-using-here-string-768x190.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">Upon script execution, the file contents are shown in the output.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"7_Using_File_Descriptor\"><\/span>7. Using File Descriptor<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">This example shows how to use a <strong>file descriptor<\/strong> within a <code class=\"\" data-line=\"\">while<\/code> loop to read a file line. The file descriptor in Bash is a non-negative unique identifier for an open file or input\/output stream. It represents what is connected to the shell script whether it is a file, network connection, or terminal.<\/p>\n<p style=\"text-align: justify;\">The reserved file descriptors are as follows:<\/p>\n<ul>\n<li style=\"text-align: justify;\"><strong>File Descriptor 0 (standard input):<\/strong> The stream from which a command reads its input data. It is connected to the keyboard by default.<\/li>\n<li style=\"text-align: justify;\"><strong>File Descriptor 1 (standard output):<\/strong> The stream to which a command writes its output data. It is connected to the terminal by default.<\/li>\n<li style=\"text-align: justify;\"><strong>File Descriptor 2 (standard error):<\/strong> The stream from which a command writes its error messages. It is connected to the terminal by default.<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">The file descriptors beyond <strong>2<\/strong> are used to represent other streams like an open file or network connections.<\/p>\n<p style=\"text-align: justify;\">In the following script, the <strong>file<\/strong> <strong>descriptor 3<\/strong> is used for the file named \u201cfile.txt\u201d to read the contents of the file:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nwhile IFS= read -r -u3 line; do\necho &quot;$line&quot;\ndone 3&lt; file.txt<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The script initiates a <strong>while loop<\/strong> that reads each line from the file descriptor 3 <code class=\"\" data-line=\"\">(u3)<\/code> and stores it in the variable <strong>line<\/strong>. The IFS is set to <strong>empty string<\/strong> to avoid unintended word spitting and the <code class=\"\" data-line=\"\">-r<\/code> option preserves the backslash characters. <code class=\"\" data-line=\"\">echo &quot;$line&quot;<\/code> prints the current read line of the file. <code class=\"\" data-line=\"\">done 3&lt; file.txt<\/code> redirects the &#8220;file.txt&#8221; to the <strong>file descriptor 3<\/strong> which means the <code class=\"\" data-line=\"\">read<\/code> command will read from this file.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35097\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9.reading-file-using-file-descriptor.png\" alt=\"reading file line using file descriptor within a while loop\" width=\"825\" height=\"203\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9.reading-file-using-file-descriptor.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9.reading-file-using-file-descriptor-300x74.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/9.reading-file-using-file-descriptor-768x189.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p>Every line that was read from the <strong>file.txt<\/strong> is shown in the image.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"8_Using_Process_Substitution\"><\/span>8. Using Process Substitution<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">Process substitution uses <code class=\"\" data-line=\"\">&lt;(...)<\/code> syntax to create a temporary file from the output of a command that allows direct use as input for other commands. In this example, for reading file lines of the <strong>file.txt<\/strong>, process substitution is used. Follow the script to know more about it:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nwhile IFS= read -r line; do\necho &quot;$line&quot;\ndone &lt; &lt;(cat file.txt)<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p><code class=\"\" data-line=\"\">while IFS= read -r line; do<\/code> starts a while loop that reads each line from the input source preserving the whitespaces and prints the read line in the terminal. <code class=\"\" data-line=\"\">done &lt; &lt;(cat file.txt)<\/code> uses process substitution <code class=\"\" data-line=\"\">&lt;(...)<\/code> that creates a temporary file-like stream from the output of the <code class=\"\" data-line=\"\">cat file.txt<\/code> command and redirects the output to the standard input of the while loop.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35098\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10.reading-file-using-process-substitution.png\" alt=\"reading file line using process substitution\" width=\"825\" height=\"201\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10.reading-file-using-process-substitution.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10.reading-file-using-process-substitution-300x73.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/10.reading-file-using-process-substitution-768x187.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p>You can see the read line from the &#8220;file.txt&#8221; file in this picture.<\/p>\n<p style=\"text-align: justify;\">To read a file line, you can also use <strong><u><a href=\"https:\/\/linuxsimply.com\/grep-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">grep command<\/a><\/u><\/strong> with the process substitution operator <code class=\"\" data-line=\"\">&lt;(...)<\/code> that takes the output of the <code class=\"\" data-line=\"\">grep<\/code> command as input and redirects it to the while loop. Copy the script below to achieve this task:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;file.txt&quot;\nwhile IFS= read -r line\ndo\necho &quot;$line&quot;\ndone &lt; &lt;(grep &quot;&quot; &quot;$filename&quot;)<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The <code class=\"\" data-line=\"\">while IFS= read -r line<\/code> initiates a while loop that reads each line from the output of the <code class=\"\" data-line=\"\">grep &quot;&quot; &quot;$filename&quot;<\/code> command and prints the lines in the terminal using the <code class=\"\" data-line=\"\">echo<\/code> command. The <code class=\"\" data-line=\"\">grep &quot;&quot;<\/code> part is used here to read all lines from the file.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35099\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11.reading-file-using-grep-command.png\" alt=\"reading file using grep command\" width=\"825\" height=\"203\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11.reading-file-using-grep-command.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11.reading-file-using-grep-command-300x74.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/11.reading-file-using-grep-command-768x189.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p>The image shows all the lines of the &#8220;file.txt&#8221; read by the <strong>grep<\/strong> command.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"How_to_Read_File_Line_in_Reverse_Order\"><\/span>How to Read File Line in Reverse Order?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Using a while loop with the <code class=\"\" data-line=\"\">tac<\/code> command, a file line can be read in reverse order. The <strong>tac<\/strong> command concatenates and displays the contents of the file reversely. The <strong>while loop<\/strong> is combined with a <code class=\"\" data-line=\"\">for<\/code> loop to read the file backward while printing the results in the original sequence. Look at the script for better understanding:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;file.txt&quot;\nlines=()\nwhile IFS= read -r line\ndo\nlines+=(&quot;$line&quot;)\ndone &lt; &lt;(tac &quot;$filename&quot;)\n\nfor ((n=${#lines[@]}-1; n&gt;=0; n--))\ndo\necho &quot;${lines[$n]}&quot;\ndone<\/code><\/pre>\n<div class=\"su-box su-box-style-default\" id=\"\" style=\"border-color:#aeb0b3;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#e1e3e6;color:#000000;border-top-left-radius:0px;border-top-right-radius:0px\">EXPLANATION<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\n<p>The script starts by assigning &#8220;file.txt&#8221; to a variable <strong>filename<\/strong>. <code class=\"\" data-line=\"\">lines=()<\/code> initializes an array to store the lines read from the <strong>file.txt<\/strong>. <code class=\"\" data-line=\"\">while IFS= read -r line<\/code> starts a while loop that reads the line from the output of the <code class=\"\" data-line=\"\">tac &quot;$filename&quot;<\/code> (reverses the lines of the file). <code class=\"\" data-line=\"\">lines+=(&quot;$line&quot;)<\/code> appends each line to the <strong>lines<\/strong> array.<\/p>\n<p><code class=\"\" data-line=\"\">done &lt; &lt;(tac &quot;$filename&quot;)<\/code> uses process substitution <code class=\"\" data-line=\"\">&lt;(...)<\/code> to treat the output of <code class=\"\" data-line=\"\">tac &quot;$filename&quot;<\/code> as a file and then redirects that output to the standard input of the while loop.<\/p>\n<p>After that, a <code class=\"\" data-line=\"\">for<\/code> loop is initialized to iterate through the <strong>lines<\/strong> array in reverse order. Finally, <code class=\"\" data-line=\"\">echo &quot;${lines[$n]}&quot;<\/code> prints each line to the terminal in its original order.<\/p>\n<\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35100\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12.reading-file-in-reverse-order.png\" alt=\"reading file in reverse order\" width=\"825\" height=\"202\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12.reading-file-in-reverse-order.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12.reading-file-in-reverse-order-300x73.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/12.reading-file-in-reverse-order-768x188.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<p style=\"text-align: justify;\">You can see all the lines from the <strong>file.txt<\/strong> in the original order but it is read in the reverse order.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Common_Issues_in_Bash_File_Reading_With_%E2%80%9Cwhile%E2%80%9D_Loop\"><\/span>Common Issues in Bash File Reading With \u201cwhile\u201d Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">When reading a file line by line, you can come across certain common issues, such as handling special characters or empty lines. The solutions to these issues are covered in this section.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Dealing_With_Empty_Lines\"><\/span>Dealing With Empty Lines<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">A file can contain empty lines and the <code class=\"\" data-line=\"\">read<\/code> command within a <strong>while<\/strong> loop by default can read a file including those empty lines. If you want to skip the empty lines from being read, you can follow the script below:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">#!\/bin\/bash\nfilename=&quot;file.txt&quot;\nwhile IFS= read -r line; do\n\n# Skip empty lines\nif [[ -n &quot;$line&quot; ]]; then\n\n# Process non-empty lines\necho &quot;$line&quot;\nfi\ndone &lt; &quot;$filename&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\">This script will only show the lines with content skipping the empty lines:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-35101\" src=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/13.skipping-empty-lines.png\" alt=\"skipping empty lines during reading a file using while loop\" width=\"825\" height=\"153\" srcset=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/13.skipping-empty-lines.png 825w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/13.skipping-empty-lines-300x56.png 300w, https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/13.skipping-empty-lines-768x142.png 768w\" sizes=\"(max-width: 825px) 100vw, 825px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Managing_Special_Characters\"><\/span>Managing Special Characters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">By default, the <code class=\"\" data-line=\"\">read<\/code> command within a <strong>while<\/strong> loop can not read backslash character <code class=\"\" data-line=\"\">\\<\/code>. So to preserve the backslashes, you can use the <code class=\"\" data-line=\"\">-r<\/code> option with the read command inside the while loop. Check <a href=\"#method_1\"><strong>method 1<\/strong><\/a> for the bash script to manage special characters like <strong>\\<\/strong>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Practice_Tasks_on_How_to_Read_File_Line_Using_Bash_%E2%80%9Cwhile%E2%80%9D_Loop\"><\/span>Practice Tasks on How to Read File Line Using Bash \u201cwhile\u201d Loop<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Do the following tasks to sharpen your understanding of how to read a file line using a while loop:<\/p>\n<ol>\n<li>Make a bash script that displays each read line of the &#8220;file.txt&#8221; along with its line numbers.<\/li>\n<li>Write a bash script that reads the &#8220;file.txt&#8221; and prints only the non-empty lines.<\/li>\n<li>Create a file that contains the names of 7 days of the week. Now, read the names of the file using a while loop.<\/li>\n<li>Generate a bash script that prints only the file line containing a specific keyword.<\/li>\n<li>Take a file of your choice, count the number of lines present in that file, and print it in the terminal using bash while loop.<\/li>\n<\/ol>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">To sum up, I have finished discussing the 8 cases for using a while loop to read a file line by line. Each technique is explained in detail in the demonstration above. I hope this has improved your understanding of using a while loop in various ways to read file data. Have a great day!<\/p>\n<h2><span class=\"ez-toc-section\" id=\"People_Also_Ask\"><\/span>People Also Ask<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"How_to_skip_empty_lines_or_lines_starting_with_specific_characters\"><\/span>How to skip empty lines or lines starting with specific characters?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">You can use an <code class=\"\" data-line=\"\">if<\/code> conditional statement inside the while loop to skip empty lines or lines starting with specific characters. Here\u2019s an example bash script for this:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">while read line; do\nif [[ -n &quot;$line&quot; &amp;&amp; ! &quot;$line&quot; =~ ^@ ]]; then\n# process non-empty lines not starting with @\nfi\ndone &lt; file.txt<\/code><\/pre>\n<p style=\"text-align: justify;\">While reading file lines, this script skips the empty lines and lines containing the specific character \u201c@\u201d of the &#8220;file.txt&#8221;.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_does_the_while_loop_read_a_file_line_by_line_in_Bash\"><\/span>How does the while loop read a file line by line in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">The while loop reads a file line by line in Bash using the <code class=\"\" data-line=\"\">read<\/code> command. Check out the basic syntax for this task:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">while IFS= read -r line; do\n# Process the current line here\ndone &lt; &quot;filename&quot;<\/code><\/pre>\n<p style=\"text-align: justify;\">This script reads a file preserving leading and trailing whitespaces and backslash characters.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"How_to_exit_the_loop_early_based_on_a_condition\"><\/span>How to exit the loop early based on a condition?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">To break a while loop early based on a condition, you can use a <code class=\"\" data-line=\"\">break<\/code> statement inside the condition. Here\u2019s an example bash script for this:<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">while read line; do\nif [[ &quot;$line&quot; == &quot;STOP&quot; ]]; then\nbreak\nfi\n# process lines\ndone &lt; file.txt<\/code><\/pre>\n<p style=\"text-align: justify;\">The script exits the loop when the word &#8220;STOP&#8221; appears on a line while it is reading the lines from the &#8220;file.txt&#8221;.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_read_command_in_Bash\"><\/span>What is read command in Bash?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p style=\"text-align: justify;\">In Bash, a <code class=\"\" data-line=\"\">read<\/code> command is a powerful tool to read input from a user or a file. It can be used to read a file line by line.<\/p>\n<p><script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": {\n    \"@type\": \"Question\",\n    \"name\": \"What is read command in Bash?\",\n    \"acceptedAnswer\": {\n      \"@type\": \"Answer\",\n      \"text\": \"In Bash, a read command is a powerful tool to read input from a user or a file. It can be used to read a file line by line.\"\n    }\n  }\n}\n<\/script><\/p>\n<p><span style=\"font-size: 18pt; color: #003366;\"><strong>Related Articles<\/strong><\/span><\/p>\n<ul>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/while-true-loop\/\" target=\"_blank\" rel=\"noopener\">&#8220;while true&#8221; Loop in Bash [4 Cases]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/example\/\" target=\"_blank\" rel=\"noopener\">8 Examples of \u201cwhile\u201d Loop in Bash<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/increment-number\/\" target=\"_blank\" rel=\"noopener\">How to Increment Number Using Bash \u201cwhile\u201d Loop [8 Methods]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/infinite-loop-one-line\/\" target=\"_blank\" rel=\"noopener\">One Line Infinite \u201cwhile\u201d Loop in Bash [4 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/while-loop-continue\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash Continue with \u201cwhile\u201d Loop [7 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/break-while-loop\/\" target=\"_blank\" rel=\"noopener\">Exit \u201cwhile\u201d Loop Using \u201cbreak\u201d Statement in Bash [11 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/while-loop-one-line\/\" target=\"_blank\" rel=\"noopener\">How to Use Bash One Line \u201cwhile\u201d Loop [8 Examples]<\/a><\/strong><\/li>\n<li><strong><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/sleep\/\" target=\"_blank\" rel=\"noopener\">How to Use \u201csleep\u201d Command in Bash \u201cwhile\u201d Loop [6 Examples]<\/a><\/strong><\/li>\n<\/ul>\n<hr \/>\n<p><strong>&lt;&lt; Go Back to <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;\u201cwhile\u201d Loop in Bash&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1077955,&quot;3&quot;:{&quot;1&quot;:0},&quot;4&quot;:{&quot;1&quot;:2,&quot;2&quot;:16777215},&quot;9&quot;:1,&quot;10&quot;:1,&quot;12&quot;:0,&quot;15&quot;:&quot;Arial&quot;,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/\" target=\"_blank\" rel=\"noopener\">\u201cwhile\u201d Loop in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Loops in Bash&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/\" target=\"_blank\" rel=\"noopener\">Loops in Bash<\/a><\/span> | <span data-sheets-root=\"1\" data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;Bash Scripting Tutorial&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:1074113,&quot;3&quot;:{&quot;1&quot;:0},&quot;9&quot;:1,&quot;10&quot;:1,&quot;11&quot;:4,&quot;12&quot;:0,&quot;16&quot;:12,&quot;17&quot;:1,&quot;23&quot;:1}\" data-sheets-hyperlink=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\"><a class=\"in-cell-link\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/\" target=\"_blank\" rel=\"noopener\">Bash Scripting Tutorial<\/a><\/span><\/strong><\/p>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-center kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;center&quot;,&quot;id&quot;:&quot;35085&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases]&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\">\n            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Reading a file line by line is a must-have skill that any developer needs to learn to master bash scripting. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases]\" class=\"read-more button\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#more-35085\" aria-label=\"Read more about How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases]\">Read more<\/a><\/p>\n","protected":false},"author":314908,"featured_media":35102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[176],"class_list":["post-35085","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bash-scripting","tag-while-loop","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases] - LinuxSimply<\/title>\n<meta name=\"description\" content=\"This article contains 11 methods to read file line using a bash while loop. Also, some common issues during reading file are explained here.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases] - LinuxSimply\" \/>\n<meta property=\"og:description\" content=\"This article contains 11 methods to read file line using a bash while loop. Also, some common issues during reading file are explained here.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/\" \/>\n<meta property=\"og:site_name\" content=\"LinuxSimply\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/mitalimostakim.mou\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-25T10:25:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-31T10:15:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/Read-file-line-using-bash-while-loop.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mitu Akter Mou\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mitu Akter Mou\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"13 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/\"},\"author\":{\"name\":\"Mitu Akter Mou\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/54b35370776bb6a79376ea0135412689\"},\"headline\":\"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases]\",\"datePublished\":\"2024-01-25T10:25:11+00:00\",\"dateModified\":\"2024-03-31T10:15:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/\"},\"wordCount\":2394,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Read-file-line-using-bash-while-loop.png\",\"keywords\":[\"while loop\"],\"articleSection\":[\"Bash Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/\",\"name\":\"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases] - LinuxSimply\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Read-file-line-using-bash-while-loop.png\",\"datePublished\":\"2024-01-25T10:25:11+00:00\",\"dateModified\":\"2024-03-31T10:15:13+00:00\",\"description\":\"This article contains 11 methods to read file line using a bash while loop. Also, some common issues during reading file are explained here.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/#primaryimage\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Read-file-line-using-bash-while-loop.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2024\\\/01\\\/Read-file-line-using-bash-while-loop.png\",\"width\":400,\"height\":400,\"caption\":\"How to Read file line using bash while loop\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/read-file\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Bash Scripting Tutorial\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Loops in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"\\\"while\\\" Loop in Bash\",\"item\":\"https:\\\/\\\/linuxsimply.com\\\/bash-scripting-tutorial\\\/loop\\\/while-loop\\\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#website\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"name\":\"LinuxSimply\",\"description\":\"All About Linux\",\"publisher\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/linuxsimply.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#organization\",\"name\":\"LinuxSimply\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/LinuxSimply-New-Logo-Without-Icon.png\",\"width\":355,\"height\":48,\"caption\":\"LinuxSimply\"},\"image\":{\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/#\\\/schema\\\/person\\\/54b35370776bb6a79376ea0135412689\",\"name\":\"Mitu Akter Mou\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"url\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"contentUrl\":\"https:\\\/\\\/linuxsimply.com\\\/wp-content\\\/uploads\\\/2023\\\/09\\\/Mitu-Akter-Mou-II-96x96.png\",\"caption\":\"Mitu Akter Mou\"},\"description\":\"Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering &amp; Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/mitalimostakim.mou\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/mitu-akter-mou-915537198\\\/\"],\"url\":\"https:\\\/\\\/linuxsimply.com\\\/author\\\/mou\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases] - LinuxSimply","description":"This article contains 11 methods to read file line using a bash while loop. Also, some common issues during reading file are explained here.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/","og_locale":"en_US","og_type":"article","og_title":"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases] - LinuxSimply","og_description":"This article contains 11 methods to read file line using a bash while loop. Also, some common issues during reading file are explained here.","og_url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/","og_site_name":"LinuxSimply","article_author":"https:\/\/www.facebook.com\/mitalimostakim.mou\/","article_published_time":"2024-01-25T10:25:11+00:00","article_modified_time":"2024-03-31T10:15:13+00:00","og_image":[{"width":400,"height":400,"url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/Read-file-line-using-bash-while-loop.png","type":"image\/png"}],"author":"Mitu Akter Mou","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mitu Akter Mou","Est. reading time":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#article","isPartOf":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/"},"author":{"name":"Mitu Akter Mou","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/54b35370776bb6a79376ea0135412689"},"headline":"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases]","datePublished":"2024-01-25T10:25:11+00:00","dateModified":"2024-03-31T10:15:13+00:00","mainEntityOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/"},"wordCount":2394,"commentCount":1,"publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/Read-file-line-using-bash-while-loop.png","keywords":["while loop"],"articleSection":["Bash Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/","url":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/","name":"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases] - LinuxSimply","isPartOf":{"@id":"https:\/\/linuxsimply.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#primaryimage"},"image":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#primaryimage"},"thumbnailUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/Read-file-line-using-bash-while-loop.png","datePublished":"2024-01-25T10:25:11+00:00","dateModified":"2024-03-31T10:15:13+00:00","description":"This article contains 11 methods to read file line using a bash while loop. Also, some common issues during reading file are explained here.","breadcrumb":{"@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#primaryimage","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/Read-file-line-using-bash-while-loop.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2024\/01\/Read-file-line-using-bash-while-loop.png","width":400,"height":400,"caption":"How to Read file line using bash while loop"},{"@type":"BreadcrumbList","@id":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/read-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/linuxsimply.com\/"},{"@type":"ListItem","position":2,"name":"Bash Scripting Tutorial","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/"},{"@type":"ListItem","position":3,"name":"Loops in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/"},{"@type":"ListItem","position":4,"name":"\"while\" Loop in Bash","item":"https:\/\/linuxsimply.com\/bash-scripting-tutorial\/loop\/while-loop\/"},{"@type":"ListItem","position":5,"name":"How to Read File Line Using Bash \u201cwhile\u201d Loop [8 Cases]"}]},{"@type":"WebSite","@id":"https:\/\/linuxsimply.com\/#website","url":"https:\/\/linuxsimply.com\/","name":"LinuxSimply","description":"All About Linux","publisher":{"@id":"https:\/\/linuxsimply.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/linuxsimply.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/linuxsimply.com\/#organization","name":"LinuxSimply","url":"https:\/\/linuxsimply.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/LinuxSimply-New-Logo-Without-Icon.png","width":355,"height":48,"caption":"LinuxSimply"},"image":{"@id":"https:\/\/linuxsimply.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/linuxsimply.com\/#\/schema\/person\/54b35370776bb6a79376ea0135412689","name":"Mitu Akter Mou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","url":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","contentUrl":"https:\/\/linuxsimply.com\/wp-content\/uploads\/2023\/09\/Mitu-Akter-Mou-II-96x96.png","caption":"Mitu Akter Mou"},"description":"Hello, This is Mitu Akter Mou, currently working as a Linux Content Developer Executive at SOFTEKO for the Linuxsimply project. I hold a bachelor's degree in Biomedical Engineering from Khulna University of Engineering &amp; Technology (KUET). Experiencing new stuff and gathering insights from them seems very happening to me. My goal here is to simplify the life of Linux users by making creative articles, blogs, and video content for all of them. Read Full Bio","sameAs":["https:\/\/www.facebook.com\/mitalimostakim.mou\/","https:\/\/www.linkedin.com\/in\/mitu-akter-mou-915537198\/"],"url":"https:\/\/linuxsimply.com\/author\/mou\/"}]}},"_links":{"self":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/35085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/users\/314908"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/comments?post=35085"}],"version-history":[{"count":0,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/posts\/35085\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media\/35102"}],"wp:attachment":[{"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/media?parent=35085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/categories?post=35085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxsimply.com\/wp-json\/wp\/v2\/tags?post=35085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}