help-bash
[Top][All Lists]
Advanced

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

Re: Do `>& -` and `<& -` also work?


From: Peng Yu
Subject: Re: Do `>& -` and `<& -` also work?
Date: Mon, 10 May 2021 14:17:23 -0500

On Mon, May 10, 2021 at 1:44 PM Andreas Kusalananda Kähäri
<andreas.kahari@abc.se> wrote:
>
> On Mon, May 10, 2021 at 12:56:27PM -0500, Peng Yu wrote:
> > > Yes, they work. You can confirm it for yourself by using the loadable
> > > builtin `fdflags'
> >
> > > or by using "ls -la /proc/$$/fd" if your system has
> > > procfs.
> >
> > OK. I didn't know this.
> >
> > $ ls -ltr /dev/fd/42
> > ls: cannot access '/dev/fd/42': Bad file descriptor
> > $ exec 42<>/tmp/1.txt
> > $ ls -ltrgR /dev/fd/42
> > -rw-r--r-- 1 wheel 0 2021/05/10-10:36:16 /dev/fd/42
> > $ exec 42>& -
> > $ ls -ltr /dev/fd/42
> > ls: cannot access '/dev/fd/42': Bad file descriptor
> >
> > > > If they both work, is this made by design?
> > >
> > > Yes, I think so because it is explicitly mentioned in the manual (the
> > > section I have quoted in
> > > https://lists.gnu.org/archive/html/help-bash/2021-05/msg00005.html ).
> >
> > The manual text that you quoted does not mention that there can be
> > spaces after `&` in `[n]>&word`. So I wouldn't consider that this is
> > clearly stated in the manual. As it is written, it doesn't seem to be
> > by design. It looks more like it is a consequence of the
> > implementation, which makes it just happens like that.
> >
> > I would think lexer return `[n]>&-` as a whole may make more sense.
> > Since `>&` has the meaning of dump stdout/err to a file, that file
> > could be named as "-".
> >
> > The current lexer returns ">&" and "-" separately, making people have
> > to write `>& ./-` in order to write both stdout/err to a file named
> > `-`. This seems to be a worse syntax overall than treating `>&-` as a
> > whole, then `>& -` can just mean write stdout/err to the file "-".
>
> Did you try your ">& ./-"?  I get
>
>         $ echo hello >& ./-
>         bash: ./: ambiguous redirect

I might have tried something else. So I thought it worked.

Then, it is even more verbose. Only this works.

$ echo >& ./'-'
$ ls -gG -- -
-rw-r--r-- 1 1 2021/05/10-14:12:42 -

> What you want is probably ">- 2>&1" or "&>./-". This comes back to the
> section of the manual where it says that out of "&>" and ">&", the first
> one is preferred.

Unless, there is a plan to deprecate the second one, I don't think
this statement matters. They both work anyway. I would just ignore it
as my current goal is to understand the parsing implementation in
bash.

>  Personally, I would suggest using ">- 2>&1".

This is verbose too.

If verbosity was not an issue, then neither &> nor >& should be
introduced in the first place.

Since they were introduced, they could be made better to be not
verbose in the cases that I mentioned.

-- 
Regards,
Peng



reply via email to

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