Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document pipe, redirection, and semicolon properly. #5319

Closed
crocket opened this issue Nov 6, 2018 · 3 comments
Closed

Document pipe, redirection, and semicolon properly. #5319

crocket opened this issue Nov 6, 2018 · 3 comments
Labels
docs An issue/PR that touches or should touch the docs
Milestone

Comments

@crocket
Copy link
Contributor

crocket commented Nov 6, 2018

POSIX shell specification describes syntax in nauseating details, but those details are very disorienting. Bash documentation describes syntax well.

@zanchey zanchey added the docs An issue/PR that touches or should touch the docs label Nov 6, 2018
@zanchey zanchey added this to the fish-future milestone Nov 6, 2018
@crocket crocket changed the title Document syntax properly. Document pipe and redirection properly. Nov 6, 2018
@faho
Copy link
Member

faho commented Nov 6, 2018

It also doesn't say what happens if I chain redirections as in

cmd1 >/dev/null ^&1 or cmd1 ^&1 >/dev/null

It's done left-to-right.

Fish checks the first, sees ">/dev/null", sets stdout to go to /dev/null. It sees ^&1 and sends stderr to where stdout is going, so both go to /dev/null.

Fish checks the second, sees "^&1" and sends stderr to where stdout is at that moment going, which is the terminal (unless it's already redirected from the outside). It goes on, sees ">/dev/null" and sends stdout to /dev/null, keeping stderr pointing to the terminal.

Imagine "&1" expanding to where stdout is going at that moment.

cmd1 ^&1 | cmd2 is not the same as cmd1 | cmd2 ^&1

The redirection is done after the pipe, so stdout is redirected to the pipe, and stderr is then redirected to where stdout is going, which is the pipe. So the first pipes both. The latter applies ^&1 to cmd2. To redirect both, you need begin and end, like begin; cmd1 | cmd2; end ^&1.

Also note that 3.0 deprecates the "^" redirection in favor of the standard "2>", so that last example should be cmd1 | cmd2 2>&1.


As far as I know, this is the same as POSIX shells (e.g. bash). In general I agree that our documentation too often assumes knowledge of that.

@crocket
Copy link
Contributor Author

crocket commented Nov 6, 2018

What about cmd1 | 2>&1 cmd2?

@faho
Copy link
Member

faho commented Nov 7, 2018

cmd1 | 2>&1 cmd2

That's a syntax error, because the 2>&1 is in command position:

$ cmd | 2>&1 cmd2
fish: Expected a command, but instead found a redirection
cmd1 | 2>&1 cmd2
       ^

If we did allow redirections to appear before the command (POSIX does, it's not used much), it would apply to the stderr of "cmd2", which would send it to its stdout, which by default would be the terminal anyway.

@crocket crocket changed the title Document pipe and redirection properly. Document pipe, redirection, and semicolon properly. Nov 8, 2018
@faho faho closed this as completed in d671710 Apr 1, 2023
@faho faho modified the milestones: fish-future, fish 3.6.2 Apr 1, 2023
faho added a commit that referenced this issue Sep 8, 2023
Fixes #5319

(cherry picked from commit d671710)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs An issue/PR that touches or should touch the docs
Projects
None yet
Development

No branches or pull requests

3 participants