Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8271791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:56:06+00:00 2026-06-08T06:56:06+00:00

When combining stderr with stdout, why does 2>&1 need to come before a |

  • 0

When combining stderr with stdout, why does 2>&1 need to come before a | (pipe) but after a > myfile (redirect to file)?

To redirect stderr to stdout for file output:

  echo > myfile 2>&1

To redirect stderr to stdout for a pipe:

  echo 2>&1 | less

My assumption was that I could just do:

  echo | less 2>&1 

and it would work, but it doesn’t. Why not?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:56:07+00:00Added an answer on June 8, 2026 at 6:56 am

    A pipeline is a |-delimited list of commands. Any redirections you specify apply to the constituent commands (simple or compound), but not to the pipeline as a whole. Each pipe chains one command’s stdout to the stdin of the next by implicitly applying a redirect to each subshell before any redirects associated with a command are evaluated.

    cmd 2>&1 | less
    

    First stdout of the first subshell is redirected to the pipe from which less is reading. Next, the 2>&1 redirect is applied to the first command. Redirecting stderr to stdout works because stdout is already pointing at the pipe.

    cmd | less 2>&1
    

    Here, the redirect applies to less. Less’s stdout and stderr both presumably started out pointed at the terminal, so 2>&1 in this case has no effect.

    If you want a redirect to apply to an entire pipeline, to group multiple commands as part of a pipeline, or to nest pipelines, then use a command group (or any other compound command):

    { { cmd1 >&3; cmd2; } 2>&1 | cmd3; } 3>&2
    

    Might be a typical example. The end result is: cmd1 and cmd2‘s stderr -> cmd3; cmd2‘s stdout -> cmd3; and cmd1 and cmd3‘s stderr, and cmd3‘s stdout -> the terminal.

    If you use the Bash-specific |& pipe, things get stranger, because each of the pipeline’s stdout redirects still occur first, but the stderr redirect actually comes last. So for example:

    f() { echo out; echo err >&2; }; f >/dev/null |& cat
    

    Now, counterintuitively, all output is hidden. First stdout of f goes to the pipe, next stdout of f is redirected to /dev/null, and finally, stderr is redirected to stdout (/dev/null still).

    I recommend never using |& in Bash — it’s used here for demonstration.

    • Many of the obligatory redirection links
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just combining css files from other templates but I can figure out why
I'm combining some CSS files and writing them to a file in a separate
I' ve tried combining the module pattern with a constructor function but it seems
I am running multiple reports and combining them into a single PDF file. For
I'm finding myself in need of combining several instances of the same type. This
Sometimes I need to implement an obj's hashCode() method by combining the hashCodes of
Does combining an Enterprise Messaging solution with Web Services result in a real performance
When combining assignment with comma (something that you shouldn't do, probably), how does javascript
I've looked into combining multiple sql statements as one but cannot get it to
I'm having difficulty combining some lines in a comma delimited text file if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.