Bash: How to read one line at a time from output of a command?
91
votes
6
answers
209181
views
I am trying to read the output of a command in bash using a
while loop
.
while read -r line
do
echo "$line"
done <<< $(find . -type f)
The output I got
ranveer@ranveer:~/tmp$ bash test.sh
./test.py ./test1.py ./out1 ./test.sh ./out ./out2 ./hello
ranveer@ranveer:~/tmp$
After this I tried
$(find . -type f) |
while read -r line
do
echo "$line"
done
but it generated an error test.sh: line 5: ./test.py: Permission denied
.
So, how do I read it line by line because I think currently it is slurping the entire line at once.
Required output:
./test.py
./test1.py
./out1
./test.sh
./out
./out2
./hello
Asked by RanRag
(6035 rep)
Oct 16, 2012, 08:35 PM
Last activity: Jun 20, 2025, 06:36 AM
Last activity: Jun 20, 2025, 06:36 AM