Skip to content

Syntax improvements for redirection & pipes #110

Closed

Description

@dhasenan

Assuming I have a process that writes to both stderr and stdout, and I want to pipe both to tee, I should be able to do:

./a.out ^&1 | tee log

This ends up with 'log' being empty.

I suspect that fish is assigning the stderr input stream to the stdout sink, then assigning the stdout input stream to the pipe sink. Another layer of indirection would likely solve this -- instead of copying the stdout sink, you assign a wrapper sink instead. This would require caution to catch the case of redirecting >&2 ^&1, of course, but it would allow people to redirect and pipe at the same time and make it work.

Activity

  1. dag commented on Jan 18, 2013

    @dag
    Contributor

    I ran into this issue almost immediately so I'd love to see a solution.

  2. JanKanis commented on Jan 18, 2013

    @JanKanis
    Contributor

    Can anyone give or link to an exact description of how bash handles the interaction of redirections and pipes? I can't find it in the bash reference manual. I always thought the bash equivalent of the o.p.'s command would give the same result as fish, but it turns out that command 2>&1 | tee myfile sends both stdout and stder of command to tee.

    My (apparently wrong) mental model of redirection in bash is that cmd1 2>&1 | cmd2 does:

    • assign the value of fd 1 (the terminal) to cmd1's fd2
    • assign the value of the pipe to cmd1's fd1.
      Fish (afaik) works like that, but what does bash do exactly?
  3. gustafj commented on Jan 23, 2013

    @gustafj
    Contributor

    In the manual for Bash under Pipelines i found this:
    This connection is performed before any redirections specified by the command.
    see http://www.gnu.org/software/bash/manual/bashref.html#Pipelines

    So the redirection would be like this if my understanding is correct:

    • connect the fd1 to the pipe
    • assign the value of fd1 (the pipe) to cmd1's fd2

    It is possible to do this today in fish using special syntax 2>| ex command 2>| tee myfile
    In Bash this is equivalent to |& which is just syntactic sugar for 2>&1 |

    I did a few trials using fish, bash and tcsh (note that my tcsh dont support to only redirect stderr, as noted in that cmd):

    cmd is perl -e 'print {*STDOUT} "stdout\n"; print {*STDERR} "stderr\n";' in all below:

    tcsh > cmd | cat - > /dev/null
    stderr
    tcsh > cmd |& cat - > /dev/null
    tcsh > cmd |& cat -
    stderr
    stdout
    tcsh > "The  shell  cannot  presently  redirect  diagnostic  output without also redirecting standard output"
    
    bash-4.1$ cmd | cat - > /dev/null
    stderr
    bash-4.1$ cmd |& cat - > /dev/null
    bash-4.1$ cmd |& cat -
    stderr
    stdout
    bash-4.1$ cmd 2>&1 | cat - > /dev/null
    
    
    fish > cmd | cat - > /dev/null
    stderr
    fish > cmd 2>| cat - > /dev/null
    fish > cmd 2>| cat -
    stdout
    stderr
    fish > cmd 2>&1 | cat - > /dev/null 
    stderr
    
  4. dag commented on Jan 23, 2013

    @dag
    Contributor

    My expectation would be that 2>| would pipe stderr but not stdout. And also that if that syntax works then so should ^|, doing the same. Piping both is likely more useful, but might arguably break "The law of user focus" in that it's not intuitive.

  5. gustafj commented on Jan 23, 2013

    @gustafj
    Contributor

    I like the ^| syntax, feels more "fish-like", and i also agree that when using 2>| (or ^|) one could expect it to only pipe stderr.
    But then what syntax would be appropriate to use when piping both (which is probably what you most often want, except for when only piping stdout).

    Proposal:

    • Only stdout |, >| or 1>|
    • Only stderr ^| or 2>|
    • Both stderr & stdout ^>| or >^| or any combination of redirection followed by pipe, ex: ^&1 | or >&2 ^|
  6. dag commented on Jan 23, 2013

    @dag
    Contributor

    The good thing is that the syntax is beginning to look like a fish ...

  7. ridiculousfish commented on Jun 28, 2013

    @ridiculousfish
    Member

    Let’s try to fix this one in next-minor

  8. ridiculousfish commented on Aug 18, 2013

    @ridiculousfish
    Member

    If you can pipe stdout and stderr independently, how do pipes associate? For example:

    foo ^| bar | baz
    

    Does baz get the output of foo or bar? Anyone know how other shells handle this?

  9. timbertson commented on Aug 18, 2013

    @timbertson
    Contributor

    If we had subshells, I assume the answer would be that you'd need to do the following if you want stderr to go through multiple processes:

    $ foo ^| (bar | baz) | frob
    

    stderr goes through baz | bar, while stdout goes through frob. I thought the above would be possible in bash, but it seems you only get one pipe in bash (http://stackoverflow.com/questions/2342826/how-to-pipe-stderr-and-not-stdout)

    Is there any subshell-like thing in fish? I often want to do stuff in another dir temporarily, which I used to do in bash with (cd DIR; do-something). I miss that.

  10. sugar700 commented on Aug 18, 2013

    @sugar700
    Contributor

    @gfxmonk: But we do.

    foo ^| begin
        bar | baz
    end | frob
  11. anddam commented on Aug 18, 2013

    @anddam

    @glitchmr that's brilliant

  12. ridiculousfish commented on Aug 18, 2013

    @ridiculousfish
    Member

    It sounds like the proposed answer to my question is that a stderr-only pipe would be standalone, and not part of the larger pipeline. That is, foo ^| bar | baz is foo ^| bar and foo | baz. If you wanted to pipe stderr further, you use begin/end to create a block. That seems reasonable to me.

  13. mrshu commented on Aug 19, 2013

    @mrshu
    Contributor

    This has been a serious issue for me too so I'd love to see a solution too.

    @glitchmr that looks very fishy!

  14. ridiculousfish commented on Aug 20, 2013

    @ridiculousfish
    Member

    With the commit 4899086 , the original problem ./a.out ^&1 | tee log should be addressed. You can assign stdout to stderr, and that happens after the pipe, so that tee sees both streams.

    I want to leave this open for further discussion on syntax improvements for redirections. The current thinking is that foo | bar pipes stdout, and foo ^| bar pipes stderr. The syntax for piping both is unclear, even though there's agreement that piping both is more common than piping stderr independently.

  15. ridiculousfish commented on Aug 20, 2013

    @ridiculousfish
    Member

    My high order bit is that syntax for common redirections should be easy to learn and to remember. In particular, no common redirections should require using numbers.

    Here are four proposals for piping stdout and stderr together:

    1. foo ^| bar or foo |^ bar
    2. foo |& bar
    3. foo ^&| bar

    Individual discussion:

    foo ^| bar or foo |^ bar

    Arguments for:

    1. It's arguably intuitive. | redirects stdout, ^ redirects stderr, so ^| redirects stderr and stdout.
    2. foo | bar is already a form of syntactic sugar over foo 1>| bar, which we justify by the fact that piping stdout is so common. ^ is also sugar over foo 2>| bar. So making foo ^| bar be syntactic sugar over another common operation doesn't seem so bad.
    3. Piping stderr along with stdin is more common than piping stderr separately. The syntax should optimize for the most common case.

    Arguments against:

    1. It uses up a natural syntax for piping stderr separately. stderr can still be piped separately via foo 2>| bar.
    2. It's also arguably unintuitive. You might reasonably expect that ^| would redirect stderr alone.
    foo |& bar

    Arguments for:

    1. This is exactly the syntax used in zsh and tcsh. We could just go with that, since fish's redirections are already pretty close to POSIX syntax (and already ugly anyways). We should only break from POSIX syntax where we can improve on it.
    2. It keeps the natural syntax free for redirecting stderr separately.

    Arguments against:

    1. It is terse but unnatural. & is not used to represent stderr anywhere else in fish.
    2. It may be confused with launching a process in the background.
    foo ^&| bar
    1. This makes some intuitive sense. | redirects stdout, ^ redirects stderr, so ^&| means "redirect stderr and stdout."
    2. It keeps the natural syntax free for redirecting stderr separately.

    Arguments against:

    1. It's the most verbose, and hardest to remember.
  16. 24 remaining items

  17. balupton commented on Feb 24, 2017

    @balupton

    @ridiculousfish yeah - I was wrestling with &> myself - I ended up going with > /dev/null ^ /dev/null - the ^ /dev/null was not mentioned on the SO answer, so would be a good addition

  18. balupton commented on Jul 3, 2017

    @balupton

    with my above answer, the command > /dev/null ^ /dev/null works only for the /dev/null output - if you want to pipe stdout and stderr to the same file, it won't work, as the > and ^ will overwrite each other.

  19. Phidica commented on Jun 20, 2018

    @Phidica
    Contributor

    Being that #4394 has removed ^ as the operator for redirection of the stderr file descriptor, I think that rules out all suggestions which involved the caret (leaving, for the most part, just things involving &). I have a new suggestion which as far as I can tell hasn't been brought up in GitHub discussions before.

    How about, to redirect multiple file descriptors to the same file, we extend the syntax to allow a list of file descriptors into the redirection operator? To redirect both stdout and stderr to the same file, it would simply be 1,2>myfile. I think this feels quite intuitive, as it evokes the comma used in brace expansion, implying the behaviour will be something like {1,2}>myfile = 1>myfile 2>myfile.

    Of course, this cannot actually "expand" to 1>myfile 2>myfile because as @balupton has pointed out above this (unintuitively) causes each file descriptor to independently overwrite the file. It would have to result in 1>myfile 2>&1 or 2>myfile 1>&2 (both are equivalent), but the idea is there. Understanding brace expansion lends to an understanding of "redirection expansion" and the naive theory of what it would achieve.

    This applies similarly in the case of redirection-into-pipe: 1,2>| is suggestive of 1>| 2>| (actually an invalid syntax) but it will achieve 2>&1 |.

    In discussion above, @ridiculousfish's take is that "no common redirections should require using numbers". But with the removal of ^, redirection of stderr explicitly requires 2, so we can't really avoid numbers in common redirections any more. In order to desire redirection of stdout and stderr, a user must already know about stderr and hence already know that it is represented by 2.

    Discussion in this thread supports &> or |& as the "1 and 2" redirection or pipe. But that would effectively limit us to only having a convenient syntax to pair 1 with 2. #3303 is an open discussion of redirection involving file descriptors 3 and above, perhaps in some way where they just open on demand when first used. What if we want to pair redirection of 1 with 3? 2 with 4? I think that a list syntax (1,3>, 2,4>) would be a good generic syntax that is prepared for a possible future where redirection with higher file descriptors is more practical.

    (And if we really wanted to save another character, then 1 could be implicit around a comma. So 1,2> could be done with ,2> or 2,> which are then just one character away from &>.)

  20. JoshCheek commented on Jun 20, 2018

    @JoshCheek

    A bit late, but a note for @balupton, you can get it to work for files with

    $ bash -c 'echo out; echo err >&2' > file ^ /dev/stdout`
    $ cat file
    out
    err
  21. balupton commented on Apr 9, 2019

    @balupton

    What is the status of this now that #4394 landed in v3

  22. faho commented on Apr 9, 2019

    @faho
    Member

    Yup, this should be closed.

    An overview of the current situation:

    • ^ is deprecated and can already be turned off. Use 2>.
    • 2>| pipes just stderr.
    • 2>&1 | pipes both. (In my experience, this is rarely necessary)
  23. modified the milestones: next-major, fish 3.0.0 on Apr 9, 2019
  24. Phidica commented on Apr 9, 2019

    @Phidica
    Contributor

    Should I open a new issue with my suggestion then? I think it's an interesting idea for extending the redirection syntax

  25. faho commented on Apr 9, 2019

    @faho
    Member

    Honestly, I don't think shortcuts are needed here. Which is also why we stepped away from ^ - it looks a teensy bit nicer, at the cost of an additional special character.

    If you want to redirect both stdout and stderr, use >/path/to/file 2>&1.

  26. floam commented on Apr 9, 2019

    @floam
    Member

    Well, I like the {1,2}> idea. It's actually consistent with expansion. It's easier to understand than the pointing 2 at &1 stuff IMO. I've actually tried exactly that and hoped it'd work in the past. I say open up an issue!

  27. frankseide commented on Mar 29, 2020

    @frankseide

    I have been reading several of these issues, and I still cannot figure out how to redirect both stdout and stderr into tee in the fish shell.

  28. faho commented on Mar 29, 2020

    @faho
    Member

    @frankseide In fish 3.1 &| is shorthand.

    Otherwise 2>&1 |.

    So:

    whatever 2>&1 | tee
    # or
    whatever &| tee

    In future, please don't comment on random issues. Instead open a new one or ask on https://gitter.im/fish-shell/fish-shell.

  29. locked as resolved and limited conversation to collaborators on Jun 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions