What does the following loop do: while read i; echo $i; done < file.txt?

Study for the OSCP Linux Exam. Use our flashcards and multiple-choice questions to test your skills. Each query comes with detailed hints and explanations to enhance your preparedness. Get ready to conquer the exam!

Multiple Choice

What does the following loop do: while read i; echo $i; done < file.txt?

Explanation:
The loop uses read to pull input line by line into a variable named i, with the file contents provided through input redirection from file.txt. On each iteration, echo prints the current value of i, so every line from the file is output one per line. This is a common pattern for processing files line by line in a shell. Note: because the variable is expanded unquoted, leading/trailing spaces and multiple spaces can be altered by word splitting. If you need to preserve the exact line content, use echo "$i" or printf '%s\n' "$i".

The loop uses read to pull input line by line into a variable named i, with the file contents provided through input redirection from file.txt. On each iteration, echo prints the current value of i, so every line from the file is output one per line. This is a common pattern for processing files line by line in a shell.

Note: because the variable is expanded unquoted, leading/trailing spaces and multiple spaces can be altered by word splitting. If you need to preserve the exact line content, use echo "$i" or printf '%s\n' "$i".

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy