Skip to content

Commit

Permalink
docs: Chapter on combining redirections
Browse files Browse the repository at this point in the history
Fixes #5319
  • Loading branch information
faho committed Apr 1, 2023
1 parent df3f2d6 commit d671710
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions doc_src/language.rst
Expand Up @@ -238,6 +238,37 @@ As a convenience, the pipe ``&|`` redirects both stdout and stderr to the same p

.. [#] A "pager" here is a program that takes output and "paginates" it. ``less`` doesn't just do pages, it allows arbitrary scrolling (even back!).
Combining pipes and redirections
--------------------------------

It is possible to use multiple redirections and a pipe at the same time. In that case, they are read in this order:

1. First the pipe is set up.
2. Then the redirections are evaluated from left-to-right.

This is important when any redirections reference other file descriptors with the ``&N`` syntax. When you say ``>&2``, that will redirect stdout to where stderr is pointing to *at that time*.

Consider this helper function::

# Just make a function that prints something to stdout and stderr
function print
echo out
echo err >&2
end

Now let's see a few cases::

# Redirect both stderr and stdout to less
# (can also be spelt as `&|`)
print 2>&1 | less

# Show the "out" on stderr, silence the "err"
print >&2 2>/dev/null
# Silence both
print >/dev/null 2>&1

.. _syntax-job-control:

Job control
Expand Down

0 comments on commit d671710

Please sign in to comment.