Source From Here
Question
I am trying to read the output of a command in bash using a while loop:
- test.sh
The output I got:
After this I tried:
- test2.sh
but it generated an error "
Error: Must provide process id to debug" So, how do I read it line by line because I think currently it is slurping the entire line at once.
How-To
There's a mistake, you need < <(command) not <<<$(command). < <( ) is a Process Substitution, $() is a command substitution and <<< is a here-string. So you can do it this way:
- test3.sh
The execution result:
Another approach using Process Substitution:
- test4.sh
Some advantages of process substitution:
Question
I am trying to read the output of a command in bash using a while loop:
- test.sh
- #!/bin/bash
- while read -r line
- do
- echo "$line"
- done <<< $(find . -type f)
After this I tried:
- test2.sh
- #!/bin/bash
- $(find . -type f) |
- while read -r line
- do
- echo "$line"
- done
How-To
There's a mistake, you need < <(command) not <<<$(command). < <( ) is a Process Substitution, $() is a command substitution and <<< is a here-string. So you can do it this way:
- test3.sh
- #!/bin/bash
- find . -type f |
- while read -r line; do
- echo "$line"
- done
Another approach using Process Substitution:
- test4.sh
- #!/bin/bash
- while read -r line; do
- echo "$line"
- done < <(find . -type f)
沒有留言:
張貼留言