Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

8
  • 9
    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.) Commented Apr 29, 2013 at 5:56
  • 1
    Thanks for the edit, @gniourf_gniourf -- you learn somthing new every day! Commented Sep 27, 2015 at 15:36
  • @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! Commented Oct 18, 2022 at 5:35
  • 2
    @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.) Commented Oct 18, 2022 at 16:14
  • 2
    @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! Commented Oct 19, 2022 at 14:12