Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why should piping be avoided in bash scripts?

Tags:

bash

pipe

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?

like image 520
realape Avatar asked Dec 03 '25 13:12

realape


1 Answers

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.

like image 103
marcolz Avatar answered Dec 07 '25 19:12

marcolz