Read a file line by line assigning the value to a variable. How to read each line of a file 1 at a time in BASH. Feed Content of a File to a Bash Script. Bash: Read File Line By Line – While Read Line Loop Posted on Tuesday December 27th, 2016 Sunday March 19th, 2017 by admin The while loop is the best way to read a file line by line in Linux. How To Read a Linux File Line by Line How To Read a File Line by Line. Common Errors with For Loops. One of the most common errors when using scripts bash on GNU/Linux is. While Loop Example. The while loop remains the most appropriate and easiest way to read. For Loop Example. I want to read a file and save it in variable, but I need to keep the variable and not just print out the file. How can I do this? I have written this script but it isn't quite what I needed: #!/b.
Active27 days ago
How do I iterate through each line of a text file with Bash?
With this script:
I get this output on the screen:
(Later I want to do something more complicated with
$p
than just output to the screen.)The environment variable SHELL is (from env):
/bin/bash --version
output:cat /proc/version
output:The file peptides.txt contains:
Peter MortensenPeter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
12 Answers
One way to do it is:
As pointed out in the comments, this has the side effects of trimming leading whitespace, interpretting backslash sequences, and skipping the trailing line if it's missing a terminating linefeed. If these are concerns, you can do:
Exceptionally, if the loop body may read from standard input, you can open the file using a different file descriptor:
Here, 10 is just an arbitrary number (different from 0, 1, 2).
that other guy80.2k88 gold badges9292 silver badges132132 bronze badges
Bruno De FraineBruno De Fraine32.1k88 gold badges4747 silver badges5858 bronze badges
rogerdpack37.7k1919 gold badges145145 silver badges270270 bronze badges
Warren Young![Bash read line from file Bash read line from file](/uploads/1/2/6/4/126412413/354938305.jpg)
32.1k77 gold badges7373 silver badges8888 bronze badges
Option 1a: While loop: Single line at a time: Input redirection
Option 1b: While loop: Single line at a time:
Open the file, read from a file descriptor (in this case file descriptor #4).
Open the file, read from a file descriptor (in this case file descriptor #4).
Option 2: For loop: Read file into single variable and parse.
This syntax will parse 'lines' based on any white space between the tokens. This still works because the given input file lines are single-word tokens. If there were more than one token per line, then this method would not work. Also, reading the full file into a single variable is not a good strategy for large files.
Stan GravesStan GravesThis syntax will parse 'lines' based on any white space between the tokens. This still works because the given input file lines are single-word tokens. If there were more than one token per line, then this method would not work. Also, reading the full file into a single variable is not a good strategy for large files.
5,73922 gold badges1414 silver badges1414 bronze badges
This is no better than other answers, but is one more way to get the job done in a file without spaces (see comments). I find that I often need one-liners to dig through lists in text files without the extra step of using separate script files.
This format allows me to put it all in one command-line. Change the 'echo $word' portion to whatever you want and you can issue multiple commands separated by semicolons. The following example uses the file's contents as arguments into two other scripts you may have written.
Or if you intend to use this like a stream editor (learn sed) you can dump the output to another file as follows.
I've used these as written above because I have used text files where I've created them with one word per line. (See comments) If you have spaces that you don't want splitting your words/lines, it gets a little uglier, but the same command still works as follows:
This just tells the shell to split on newlines only, not spaces, then returns the environment back to what it was previously. At this point, you may want to consider putting it all into a shell script rather than squeezing it all into a single line, though.
Best of luck!
mightypilemightypile4,59822 gold badges2525 silver badges3030 bronze badges
A few more things not covered by other answers:
This approach is better than
command ... | while read -r line; do ...
because the while loop here runs in the current shell rather than a subshell as in the case of the latter. See the related post A variable modified inside a while loop is not remembered.Related read: BashFAQ/020 - How can I find and safely handle file names containing newlines, spaces or both?
Based on @chepner's answer here:
-u
is a bash extension. For POSIX compatibility, each call would look something like read -r X <&3
.If the file ends with an incomplete line (newline missing at the end), then:
or
And then
Related posts:
codeforestercodeforester20.4k88 gold badges4646 silver badges7777 bronze badges
Use a while loop, like this:
Notes:
- If you don't set the
IFS
properly, you will lose indentation.
14.6k55 gold badges6565 silver badges8787 bronze badges
If you don't want your read to be broken by newline character, use -
Then run the script with file name as parameter.
Anjul SharmaAnjul Sharma
Suppose you have this file:
There are four elements that will alter the meaning of the file output read by many Bash solutions:
- The blank line 4;
- Leading or trailing spaces on two lines;
- Maintaining the meaning of individual lines (i.e., each line is a record);
- The line 6 not terminated with a CR.
If you want the text file line by line including blank lines and terminating lines without CR, you must use a while loop and you must have an alternate test for the final line.
Here are the methods that may change the file (in comparison to what
cat
returns):1) Lose the last line and leading and trailing spaces:
(If you do
while IFS= read -r p; do printf '%sn' '$p'; done </tmp/test.txt
instead, you preserve the leading and trailing spaces but still lose the last line if it is not terminated with CR)2) Using process substitution with
cat
will reads the entire file in one gulp and loses the meaning of individual lines:(If you remove the
'
from $(cat /tmp/test.txt)
you read the file word by word rather than one gulp. Also probably not what is intended...)The most robust and simplest way to read a file line-by-line and preserve all spacing is:
If you want to strip leading and trading spaces, remove the
IFS=
part:(A text file without a terminating
n
, while fairly common, is considered broken under POSIX. If you can count on the trailing n
you do not need || [[ -n $line ]]
in the while
loop.) More at the BASH FAQ
dawgdawg62.8k1313 gold badges9292 silver badges161161 bronze badges
SineSine
Here is my real life example how to loop lines of another program output, check for substrings, drop double quotes from variable, use that variable outside of the loop. I guess quite many is asking these questions sooner or later.
Declare variable outside of the loop, set value and use it outside of loop requires done <<< '$(...)' syntax. Application need to be run within a context of current console. Quotes around the command keeps newlines of output stream.
Loop match for substrings then reads name=value pair, splits right-side part of last = character, drops first quote, drops last quote, we have a clean value to be used elsewhere.
WhomeWhome8,07533 gold badges3131 silver badges5656 bronze badges
This is coming rather very late, but with the thought that it may help someone, i am adding the answer. Also this may not be the best way.
head
command can be used with -n
argument to read n lines from start of file and likewise tail
command can be used to read from bottom. Now, to fetch nth line from file, we head n lines, pipe the data to tail only 1 line from the piped data. madD7madD7
@Peter: This could work out for you-
This would return the output-
Alan JebakumarAlan Jebakumar
protected by InianApr 17 '18 at 7:23
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?
![Bash Read Two Lines From File Bash Read Two Lines From File](/uploads/1/2/6/4/126412413/139991455.png)