You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
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:
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.
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 ^|
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.
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.
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.
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:
foo ^| bar or foo |^ bar
foo |& bar
foo ^&| bar
Individual discussion:
foo ^| bar or foo |^ bar
Arguments for:
It's arguably intuitive. | redirects stdout, ^ redirects stderr, so ^| redirects stderr and stdout.
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.
Piping stderr along with stdin is more common than piping stderr separately. The syntax should optimize for the most common case.
Arguments against:
It uses up a natural syntax for piping stderr separately. stderr can still be piped separately via foo 2>| bar.
It's also arguably unintuitive. You might reasonably expect that ^| would redirect stderr alone.
foo |& bar
Arguments for:
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.
It keeps the natural syntax free for redirecting stderr separately.
Arguments against:
It is terse but unnatural. & is not used to represent stderr anywhere else in fish.
It may be confused with launching a process in the background.
foo ^&| bar
This makes some intuitive sense. | redirects stdout, ^ redirects stderr, so ^&| means "redirect stderr and stdout."
It keeps the natural syntax free for redirecting stderr separately.
@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
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.
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 &>.)
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.
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!
Activity
dag commentedon Jan 18, 2013
I ran into this issue almost immediately so I'd love to see a solution.
JanKanis commentedon Jan 18, 2013
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 myfilesends both stdout and stder ofcommandtotee.My (apparently wrong) mental model of redirection in bash is that
cmd1 2>&1 | cmd2does:Fish (afaik) works like that, but what does bash do exactly?
gustafj commentedon Jan 23, 2013
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:
It is possible to do this today in fish using special syntax
2>|excommand 2>| tee myfileIn Bash this is equivalent to
|&which is just syntactic sugar for2>&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):
cmdisperl -e 'print {*STDOUT} "stdout\n"; print {*STDERR} "stderr\n";'in all below:dag commentedon Jan 23, 2013
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.gustafj commentedon Jan 23, 2013
I like the
^|syntax, feels more "fish-like", and i also agree that when using2>|(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:
|,>|or1>|^|or2>|^>|or>^|or any combination of redirection followed by pipe, ex:^&1 |or>&2 ^|dag commentedon Jan 23, 2013
The good thing is that the syntax is beginning to look like a fish ...
ridiculousfish commentedon Jun 28, 2013
Let’s try to fix this one in next-minor
ridiculousfish commentedon Aug 18, 2013
If you can pipe stdout and stderr independently, how do pipes associate? For example:
Does baz get the output of foo or bar? Anyone know how other shells handle this?
timbertson commentedon Aug 18, 2013
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:
stderr goes through
baz | bar, while stdout goes throughfrob. 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.sugar700 commentedon Aug 18, 2013
@gfxmonk: But we do.
anddam commentedon Aug 18, 2013
@glitchmr that's brilliant
ridiculousfish commentedon Aug 18, 2013
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 | bazisfoo ^| barandfoo | baz. If you wanted to pipe stderr further, you use begin/end to create a block. That seems reasonable to me.mrshu commentedon Aug 19, 2013
This has been a serious issue for me too so I'd love to see a solution too.
@glitchmr that looks very fishy!
ridiculousfish commentedon Aug 20, 2013
With the commit 4899086 , the original problem
./a.out ^&1 | tee logshould 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 | barpipes stdout, andfoo ^| barpipes stderr. The syntax for piping both is unclear, even though there's agreement that piping both is more common than piping stderr independently.ridiculousfish commentedon Aug 20, 2013
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:
foo ^| barorfoo |^ barfoo |& barfoo ^&| barIndividual discussion:
foo ^| barorfoo |^ barArguments for:
|redirects stdout,^redirects stderr, so^|redirects stderr and stdout.foo | baris already a form of syntactic sugar overfoo 1>| bar, which we justify by the fact that piping stdout is so common.^is also sugar overfoo 2>| bar. So makingfoo ^| barbe syntactic sugar over another common operation doesn't seem so bad.Arguments against:
foo 2>| bar.^|would redirect stderr alone.foo |& barArguments for:
Arguments against:
foo ^&| bar|redirects stdout,^redirects stderr, so^&|means "redirect stderr and stdout."Arguments against:
24 remaining items
balupton commentedon Feb 24, 2017
@ridiculousfish yeah - I was wrestling with
&>myself - I ended up going with> /dev/null ^ /dev/null- the^ /dev/nullwas not mentioned on the SO answer, so would be a good additionbalupton commentedon Jul 3, 2017
with my above answer, the
command > /dev/null ^ /dev/nullworks only for the/dev/nulloutput - if you want to pipe stdout and stderr to the same file, it won't work, as the>and^will overwrite each other.Phidica commentedon Jun 20, 2018
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>myfilebecause as @balupton has pointed out above this (unintuitively) causes each file descriptor to independently overwrite the file. It would have to result in1>myfile 2>&1or2>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 of1>| 2>|(actually an invalid syntax) but it will achieve2>&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 requires2, 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
1could be implicit around a comma. So1,2>could be done with,2>or2,>which are then just one character away from&>.)JoshCheek commentedon Jun 20, 2018
A bit late, but a note for @balupton, you can get it to work for files with
balupton commentedon Apr 9, 2019
What is the status of this now that #4394 landed in v3
faho commentedon Apr 9, 2019
Yup, this should be closed.
An overview of the current situation:
^is deprecated and can already be turned off. Use2>.2>|pipes just stderr.2>&1 |pipes both. (In my experience, this is rarely necessary)Phidica commentedon Apr 9, 2019
Should I open a new issue with my suggestion then? I think it's an interesting idea for extending the redirection syntax
faho commentedon Apr 9, 2019
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.floam commentedon Apr 9, 2019
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!frankseide commentedon Mar 29, 2020
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.
faho commentedon Mar 29, 2020
@frankseide In fish 3.1
&|is shorthand.Otherwise
2>&1 |.So:
In future, please don't comment on random issues. Instead open a new one or ask on https://gitter.im/fish-shell/fish-shell.