Suppress stderr from a command
Discard a command's error output (without affecting stdout or exit code) — for noisy tools whose warnings clutter scripts and CI logs.
How to suppress stderr from a command in each shell
noisy-cmd 2>/dev/null`/dev/null` is the canonical Unix bit-bucket. Both stderr-only (`2>`) and stdout-only (`>`) variants exist; combined `&>/dev/null` discards BOTH (bash 4+ / zsh; not POSIX). Exit code is preserved.
noisy-cmd 2>/dev/nullnoisy-cmd 2>/dev/nullnoisy-cmd 2>$null`$null` is the pwsh null object (NOT the string `"$null"`). For ALL streams (suppress everything): `noisy-cmd *>$null`. For cmdlets, prefer `-ErrorAction SilentlyContinue` over redirect — `Get-Item missing -ErrorAction SilentlyContinue` does not even GENERATE the error stream.
noisy-cmd 2>nul`nul` is the Windows null device (note: NO leading slash — it's not `/dev/null`). Equivalent forms: `2>nul`, `1>nul` (suppress stdout), `>nul 2>&1` (suppress both).
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- **Exit code preservation**: `cmd 2>/dev/null` discards stderr BUT preserves `$?` — the script sees the original exit code, just without the noise. This matters for the "test for existence" idiom: `command -v node 2>/dev/null` returns 0 if node is in PATH, 1 if not — suppressing stderr keeps the rc check working while hiding the "node: command not found" message. Same in pwsh: `$LASTEXITCODE` is independent of stream redirection.
- pwsh `-ErrorAction` parameter is the BETTER alternative for cmdlets: `Get-Item missing -ErrorAction SilentlyContinue` (or `-ErrorAction Ignore`) suppresses the error AT THE SOURCE — the error object is never generated, doesn't enter `$Error`, doesn't trigger try/catch. By contrast `Get-Item missing 2>$null` STILL generates the error (it goes into `$Error` and persists), just doesn't print to console. For long-running pwsh scripts that loop over errors, the difference is real — `$Error` accumulates and eventually pressures memory. Other values: `Continue` (default, prints + persists), `Stop` (terminating), `Inquire` (interactive prompt), `Suspend` (workflow-only).
- Combined-redirect forms: bash/zsh `&>/dev/null` (both streams to /dev/null, bash 4+); POSIX-portable `>/dev/null 2>&1` (works in sh, dash, busybox). pwsh `*>$null` (all six streams). cmd `>nul 2>&1` (order matters per /task/merge-stderr-with-stdout — `>nul 2>&1` correct; `2>&1 >nul` leaves stderr on console). fish `&>/dev/null` was added fish 3.0; older fish needs `>/dev/null ^/dev/null`. The portable form across bash/zsh/fish is `>/dev/null 2>&1`.
- Common anti-patterns: (1) `cmd 2>/dev/null || handler` — the handler runs on ANY non-zero rc, including legitimate "no, file doesn't exist" results that you swallowed. Better: explicitly test for the condition you expected (`[ -f file ] || handler`). (2) `cmd > /dev/null 2>&1` to "ignore everything" — fine for fire-and-forget, but you also discard the EXIT CODE check; if you care whether the command worked, capture rc first. (3) Suppressing stderr to hide a real bug: a tool that prints "deprecation: use --new-flag" to stderr should be FIXED upstream, not silenced — silenced warnings are how production breakage ships.
Related commands
Related tasks
- Merge stderr into stdout— Combine a command's error stream with its normal output so a single pipe / file / variable captures BOTH — essential for log aggregation and CI capture.
- Tee command output to a file and stdout— Write a command's stdout to a file AND echo it to the terminal in one pass — for logged-but-watchable installer / build / CI runs.