Re: [Help-bash] Best way to suppress the error message in $(< nonexisten
From:
Greg Wooledge
Subject:
Re: [Help-bash] Best way to suppress the error message in $(< nonexistent_file)
Date:
Wed, 28 Nov 2018 09:11:53 -0500
User-agent:
NeoMutt/20170113 (1.7.2)
On Wed, Nov 28, 2018 at 07:59:10AM -0600, Peng Yu wrote:
> > { x=$( <doesnotexist) ;} 2>&-
>
> OK. Thanks. BTW, I think that 2>&- the same as 2>/dev/null
> semantically the same. Is it so?
No. One of them redirects stderr to /dev/null and the other closes
stderr completely.
In the general case, commands that expect to be able to write error
messages to stderr will blow up if you use 2>&-. It may work HERE
since there is no external command being invoked. It's still a really
bad practice, and you SHOULD NOT get into the habit of doing it.
https://mywiki.wooledge.org/BashPitfalls#pf55