help-bash
[Top][All Lists]
Advanced

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

Re: difference between read -u fd and read <&"$fd"


From: Kerin Millar
Subject: Re: difference between read -u fd and read <&"$fd"
Date: Thu, 16 May 2024 14:58:00 +0100
User-agent: Cyrus-JMAP/3.11.0-alpha0-456-gcd147058c-fm-hotfix-20240509.001-g0aad06e4

On Thu, 16 May 2024, at 2:19 PM, Peng Yu wrote:
> On Wed, May 15, 2024 at 11:51 PM Kerin Millar <kfm@plushkava.net> wrote:
>>
>> On Thu, 16 May 2024, at 3:25 AM, Peng Yu wrote:
>> > Hi,
>> >
>> > It appears to me that read -u fd and read <&"$fd" achieve the same
>> > result. But I may miss corner cases when they may be different.
>> >
>> > Is it true that they are exactly the same?
>>
>> They are not exactly the same. To write read -u fd is to instruct the read 
>> builtin to read directly from the specified file descriptor. To write read 
>> <&"$fd" entails one invocation of the dup2 syscall to duplicate the 
>> specified file descriptor to file descriptor #0 and another invocation to 
>> restore it once read has concluded. That's measurably slower where looping 
>> over read.
>
> OK. So besides this performance difference, they are functionally the
> same. In other words, one program uses read -u "$fd" the other program
> uses read <&"$fd". The results of the two programs shall be the same?

I cannot comment on programs beyond my purview but it's almost certainly true 
that you would be hard pressed to use either method in such a way that results 
in an apparent difference in behaviour.

--
Kerin Millar



reply via email to

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