• Breaking News

    Looping through the content of a file in Bash?

    Looping through the content of a file in Bash?

    This is a very common question for everyone who works on Unix or Linux operating systems.

    The answer is quite simple, use “read” command in combination with a named pipe or an unnamed pipe.


    The code

    Here you have #2 possible options that will help you to achieve this goal. 

    They do exactly the same, so pick up which looks best suitable for your coding style:

    Option #1

    cat file.txt | while read line
      do
        echo "processing line: $line"
      done
    

    Option #2

    while read line
      do
        echo "processing line: $line"
      done < file.txt
    

    Super easy!


    No comments