35

I need to read a file using a "Do/While" loop.
How can I read the contents as a string?

Here's my code:

cat directory/scripts/tv2dbarray.txt | while read line
do
  echo "a line: $line"
done

Error:

test.sh: line 4: syntax error near unexpected token `done'
test.sh: line 4: `done'
2
  • 2
    There's nothing wrong with this code. Is there something else in the script? Perhaps another while or if or an unbalanced quote, brace, bracket or parentheses? Commented Feb 20, 2011 at 16:11
  • # or - it might be copy-pasted (with some rich text, Which is messing up the file itself) how-why-what-is this I am typing? uhm; it have (put simply) happened to me a bunch--of-times when I copy-pasting code into libreoffice, then copy-pasting it back(having colored the text) so some in-visible characters also get's copied - really weird - but it still is the same problem; to solve it you just by-hand type it(cat directory...) (for long files this is a problem) (for short snippets like this one, it's easily and quickly typed) Just needed to throw that in for any future explorer` Commented Oct 20, 2022 at 19:45

4 Answers 4

134

There's no reason to use cat here -- it adds no functionality and spawns an unnecessary process.

while IFS= read -r line; do
  echo "a line: $line"
done < file

To read the content of a file into a variable, use foo=$(<file). (Note that this trims trailing newlines.)

Sign up to request clarification or add additional context in comments.

8 Comments

I wish I could upvote this more times and ensure that it was the top result of every Google search. I phrased this question so many ways over the past hour and everytime until now I found answers along the lines of "use cat". Finally, the sane one line answer (which, unlike cat, simply works.)
Thanks for the edit, @gniourf_gniourf -- you learn somthing new every day!
@AdamLiss @ArtOfWarfare is there a way to define a variable via that method to also keep num. of "things" or "lines" ? like, "for each line do ECHO LINE but also ECHO NUMBER " ? I don't mean another for or yeah but I thought - do read have such a feature? like, read->file line by line'sCONTENTS into variable_A and at same time, read->file line by line'S NUMBER into variable_B ? and I used CAPS at times to just make it more detailed, (so, uhm - I do not scream! :) ) if not; thanks anyway - have a great day!
@WilliamMartens - Yes, but really quickly you hit the point where you have to ask yourself why you're doing so much in bash and not just using Python or another scripting language. (No, I don't know your answer but I'm sure there is one. Search around on SO - I'm sure it's been asked and answered before. Post a new question if it hasn't been.)
@WilliamMartens: i=0; while read line; do ((i=i+1)); echo "Line $i: $line"; done < file You could also cheat and use grep -n to search for something that will match every line and number the results: grep -n '' file | while read... @ArtOfWarfare I can't help it. My native language is bash. 😀️ How wonderful to see a question and answer that are still relevant 10 years later!
|
45
cat file | while read line
do
  echo "a line: $line"
done

EDIT:

To get file contents into a var use:

foo="`cat file`"

2 Comments

test.sh: line 5: syntax error near unexpected token done' test.sh: line 5: done'
4

This should work:

file=path/of/file/location/filename.txt

while IFS= read -r varname; do
    printf '%s\n' "$varname"
done < "$file"

Comments

0

try it

while read p
do 
echo $p
done <  directory/scripts/tv2dbarray.txt

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.