Artificial intelligent assistant

How to loop over the lines of a file? Say I have this file: hello world hello world This program #!/bin/bash for i in $(cat $1); do echo "tester: $i" done outputs tester: hello tester: world tester: hello tester: world I'd like to have the `for` iterate over each line individually ignoring whitespaces though, i.e. the last two lines should be replaced by tester: hello world Using quotes `for i in "$(cat $1)";` results in `i` being assigned the whole file at once. What should I change?

(9 years later:)
Both provided answers would fail on files without a newline at the end, this will effectively skip the last line, produce no errors, would lead to disaster (learned hard way:).

The best concise solution I found so far that "Just Works" (in both bash and sh):


while IFS='' read -r LINE || [ -n "${LINE}" ]; do
echo "processing line: ${LINE}"
done < /path/to/input/file.txt


For more in-depth discussion see this StackOverflow discussion: How to use "while read" (Bash) to read the last line in a file if there’s no newline at the end of the file?

Beware: this approach adds an additional newline to the last line if there is none already.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 7865132ad4a6fe12d6459a3dffb49143