Reputation: 33
Most of the time I see someone suggesting using a pipe in a bash script there is someone pointing out not to use it and instead use only one command.
Example:
find $dir -name $pattern
instead of
ls $dir | grep $pattern
Is there another reason than look to avoid pipe?
Upvotes: 3
Views: 1584
Reputation: 199
Because pipe create a new process. In your example, ls and grep are two processes and find is one. One or more pipes makes command slower. One trivial example:
$ time find Downloads -name *.pdf &>/dev/null
real 0m0.019s
user 0m0.012s
sys 0m0.004s
$ time ls Downloads | grep pdf &>/dev/null
real 0m0.021s
user 0m0.012s
sys 0m0.004s
Upvotes: 2
Reputation: 2970
There is nothing wrong with piping per se. What should be avoided is useless fork()
ing, meaning that starting a process is a relatively time-consuming thing.
If something can be done in one process, that is usually better than using two processes for the same result.
Upvotes: 3