22

I know the answer for some shells, e.g. with cmd.exe you can:

some.exe > out.txt 2>&1

How do you achieve the same for other shells (bash, ksh, tcsh, powershell, etc)?

4 Answers 4

18

Fish shell

To redirect both standard output and standard error to the file all_output.txt, you can write:

echo Hello > all_output.txt 2>&1
5
  • 3
    if piping output to next command, use 2>| instead: echo Hello 2>| less
    – Jeffrey04
    Aug 14, 2014 at 10:18
  • The piping syntax may have changed. My fish shell uses the same syntax "^&1" for both. Jan 24, 2017 at 22:53
  • 2>&1 also works — ^ is essentially fish's shorthand for >2. Note that 2>| less or ^| less by itself pipes only stderr; to pipe both our and err, do ^&1 | less Apr 30, 2017 at 9:09
  • Syntax shorthands for redirecting/piping both out and err are still under discussion github.com/fish-shell/fish-shell/issues/110 Apr 30, 2017 at 9:17
  • The ^ operator was deprecated in fish 3.0 (2018-12-28) and disabled by default in fish 3.3 (2021-06-28). Don't use it.
    – smammy
    Feb 25, 2022 at 20:27
5

For csh and tcsh

some.exec >& out.txt
1
  • for tcsh, you also have some.exec |& less
    – boatcoder
    Jan 27, 2011 at 22:21
3

In Powershell it is exactly the same:

2>&1      Sends errors to the        get-process none, powershell 2>&1
          success output stream.

(from about_Redirection).

In bash and ksh at least I also know it works this way.

It seems to be a common convention.

A quick way to find out about it is by opening the man page of the shell and do a search for &1 (with / and then typing &1). This rarely occurs in other contexts.

2

In bash you do exactly the same.

./some_exec > out 2>&1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .