Quantcast
Viewing all articles
Browse latest Browse all 7

Answer by ᄂᄀ for Bash: How to read one line at a time from output of a command?

There's no need for command substitution if you want to use pipe. read reads from stdin by default, so you just pipe into it:

find . -type f -print0 |while read -r -d '' linedo    echo "$line"done

or in one line

find . -type f -print0 | while read -r -d '' line; do echo "$line"; done

Using -print0 and -d '' is a protective measure against potential newline character in filenames, which by default indicates end of line to read (this can be changed with -d). '' used above is effectively null byte in bash (equivalent to $'\0').

Another already mentioned approach is to use find's -exec option. Most likely that would be the best option performance-wise (remember to use -exec cmd {} + variant so that it won't fork a process for each line being processed). But it really depends on what you do inside the while loop and how much data you process. The difference might be negligible or acceptable for your case.


Viewing all articles
Browse latest Browse all 7

Trending Articles