Skip to content
shellmap

Redirect stderr to a file

Capture only error output from a command, optionally merged with stdout, into a file.

How to redirect stderr to a file in each shell

Bashunix
command 2> errors.log

`2>` redirects fd 2 (stderr). Merge with stdout: `command > out.log 2>&1` — order matters: `2>&1 > out.log` does NOT merge stderr into the file.

Zshunix
command 2> errors.log
Fishunix
command 2> errors.log

Fish 3.0+ supports `2>`. Older fish used `^` for stderr (`command ^errors.log`) — that syntax is deprecated and removed.

PowerShellwindows
command 2> errors.log

PowerShell exposes six numbered streams (1=Success, 2=Error, 3=Warning, 4=Verbose, 5=Debug, 6=Information). `*>` redirects all of them; `2>&1` merges error into success.

cmd.exewindows
command 2> errors.log

Identical syntax to bash. Merge with stdout: `command > out.log 2>&1`. `2>NUL` discards stderr (the cmd equivalent of `/dev/null`).

Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.

Gotchas & notes

  • In bash the redirection order is right-to-left: `command > file 2>&1` first sends stdout to `file`, then duplicates fd 2 to the **current target of fd 1** (i.e. `file`). Swapping (`2>&1 > file`) leaves stderr on the terminal.
  • PowerShell 5 cannot redirect non-cmdlet (native exe) stderr without `2>&1`; PowerShell 7+ handles native command stderr correctly via `2>` alone.
  • Fish's `^` syntax was removed in 3.0 — scripts written for fish 2.x must be rewritten with `2>`.
  • To **append** rather than overwrite: `2>> errors.log` works the same in bash, cmd, PowerShell, and fish 3+.

Related commands

Related tasks