help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Best way to suppress the error message in $(< nonexisten


From: Pierre Gaston
Subject: Re: [Help-bash] Best way to suppress the error message in $(< nonexistent_file)
Date: Wed, 28 Nov 2018 16:12:33 +0200

On Wed, Nov 28, 2018 at 3:59 PM Peng Yu <address@hidden> wrote:

> > { x=$( <doesnotexist) ;} 2>&-
>
> OK. Thanks. BTW, I think that 2>&- the same as 2>/dev/null
> semantically the same. Is it so? Regarding the underlying operations,
> are they exactly the same or they are slightly different? My
> understanding is the former sets stderr to a null handle, but the
> latter set stderr to an existing file handle, but the file handle is
> /dev/null. So I'd think the former is a little faster. Is it? Thanks.
>
>
No it's not the same. File descriptors are like pointers to structures the
kernel uses to track
the status of files,
When you do >/dev/null  you "open" a new file descriptor that points to a
special file.
The applications then write to this file normally.

On the contrary 2>&- closes the file descriptor so your descriptor does not
refer to anything
and you get an error if you try to use the descriptor:

$ echo >&-
-bash: echo: write error: Bad file descriptor

So an application using this descriptor may eg. crash.


reply via email to

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