help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Optimising cat in pipelines


From: Dave Jennings
Subject: Re: Optimising cat in pipelines
Date: Fri, 22 Nov 2024 11:49:14 +1100
User-agent: Mozilla Thunderbird


On 22/11/24 04:42, Lawrence Velázquez wrote:
On Thu, Nov 21, 2024, at 12:55 AM, Dave Jennings wrote:
[[ $filter_option=reverse ]] && filter_fn=rev
[[ $output_option=start-only ]] && output_fn=head

As an aside, these [[...]] commands always succeed because they
treat their contents as a single nonnull operand.  They don't
recognize the "=" operand unless it is surrounded by whitespace:

        $ var=foo
        $ [[ $var=bar ]]; echo "$?"
        0
        $ [[ $var = bar ]]; echo "$?"
        1

Thanks. That was a typo. Definitely have the spaces in scripts.

My question is, does bash do any optimisation if it sees:

input | cat | output

Does it optimise the cat (and one of the pipes) away?

As Greg already said, this is not done.


I'm guessing not but it would be very nice if it did.

Such an optimization would be unsafe because bash can't know for
sure that cat merely passes data through.  It could be a wrapper
script, a loadable builtin, a function, or literally anything.

Yes. That makes sense.

So I'll use this technique in low bandwidth pipelines where the extra cat doesn't matter but in high bandwidth cases I'll craft a different path for each combination (it can explode a bit for longer pipelines but should be do-able).

Thanks, Dave.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]