Pcornel
Browse topics
Options
Recent
Favorites
Linux · Technical reference

stdin / stdout / stderr

Direct answer

Every process starts with three file descriptors: 0 stdin (input), 1 stdout (normal output) and 2 stderr (errors and diagnostics). By default all three are the terminal.

When to use this

Use it to understand why errors still appear on screen when output is redirected, or why a pipe misses messages.

First 30 seconds

1. Is the text on stdout or stderr? Test: cmd 2>/dev/null hides only stderr.

2. Need both in a pipe → 2>&1 |.

3. Service output → journal: journalctl -u <svc>.

4. ls -l /proc/<pid>/fd — where do a process's 0, 1, 2 point?

5. Adjust the redirection.

Operational verification

process inherits fd 0, 1, 2 → shell redirections change them → program reads 0 and writes 1 and 2

Evidence · next step

For Shell & Bash → stdin / stdout / stderr, continue with the first unproven Topic check: Identify fd 0/1/2 targets in the same process/session and whether each endpoint is a TTY, pipe, file, socket or closed. If that boundary is healthy, advance to the adjacent check: Capture data and diagnostics separately before parsing output programmatically. Preserve the same host/time/identity scope and rerun the original workload before accepting recovery.

Continue only after interpreting the current observation.
Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
linux
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

Platformlinux
Scope / prerequisiteUse it to understand why errors still appear on screen when output is redirected, or why a pipe misses messages.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

5 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Operational evidence path Verify boundary

parent/session fd table → inherited fd 0/1/2 → program TTY/buffering behavior → read/write syscalls → backing terminal/pipe/file/socket → consumer + exit status

Healthy evidence: fd 0/1/2 resolve to expected endpoints, data/diagnostics are separated as required, buffering is understood and the downstream consumer sees complete output.

Step 2 Verification and output interpretation Verify boundary

Existing fast checks for this Topic. - Identify fd 0/1/2 targets in the same process/session and whether each endpoint is a TTY, pipe, file, socket or closed.

  • Capture data and diagnostics separately before parsing output programmatically.
  • Account for buffering and line/block behavior when chaining commands or services.
  • Verify the consuming process/service receives the expected stream and that status is checked independently of stderr content.

Verification chain. parent/session fd table → inherited fd 0/1/2 → program TTY/buffering behavior → read/write syscalls → backing terminal/pipe/file/socket → consumer + exit status

Step 3 Commands Verify boundary
cmd 1> out 2> err
cat < input.txt
echo error >&2
Step 4 Failure modes and next checks Narrow the fault

Suspicious evidence: a program changes behavior when non-TTY, diagnostics contaminate parsed stdout, or service-manager inheritance closes/redirects a descriptor unexpectedly.

Step 5 Common mistakes Narrow the fault
  • Writing error messages to stdout.
Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points

Programs should write data to stdout and messages to stderr, so pipes and redirects keep them apart. /dev/null discards output. ls -l /proc/<pid>/fd shows where a running process's descriptors point.

Key checks
  • bash -x script.sh — trace each expanded command
  • bash -n script.sh — syntax check only
  • shellcheck script.sh — common bugs and quoting problems
  • echo $? — exit code of the last command
ReferenceWhat it is1 section · depth on demand

Core model

Inspect the actual descriptor targets of the running process rather than assuming terminal I/O. Programs can change buffering or behavior when stdout is not a TTY, may emit diagnostics to stderr while producing data on stdout, and can inherit closed or redirected descriptors from a parent/service manager.

ReferenceImportant terms & boundaries5 sections · depth on demand

What this shows and what it does not

Standard-stream evidence shows where bytes flow and how they are separated; it does not by itself establish application success or failure.

In depth

Standard input, output and error

Every process starts with three open file descriptors: 0 (standard input), 1 (standard output) and 2 (standard error). Programs write normal results to stdout and messages to stderr. That keeps errors visible even when the output goes into a file or a pipe. The bash(1) manual page documents how the shell connects them:

  • A pipe carries only stdout, unless |& is used.
  • 2>&1 duplicates stderr onto wherever stdout points at that moment.
  • /dev/stdin, /dev/stdout and /dev/stderr give programs file names for the three streams.

Descriptors

FDNameDefaultTypical redirection
0stdinTerminal keyboard< file, <<< "text", a pipe
1stdoutTerminal> file, | next-command
2stderrTerminal2> errors.log, 2>/dev/null, 2>&1

Conventions worth following in scripts

ConventionExample
Messages and progress to stderrecho "processing $f" >&2
Only results to stdoutSo the script can be used in pipes
- as a file name for stdin or stdouttar cf - dir | ssh host tar xf -
Check whether stdout is a terminal[ -t 1 ] (e.g. to enable colours)

Buffering

When stdout is a terminal, most programs flush it line by line. When it's a pipe or a file, they buffer it in larger blocks. stderr is normally unbuffered. That's why the order of output and error lines in a combined log may differ from the order on screen. stdbuf -oL or a program's own option (grep --line-buffered) changes it.

host$ ls /etc/hostname /missing
ls: cannot access '/missing': No such file or directory
/etc/hostname
host$ ls /etc/hostname /missing 2>/dev/null
/etc/hostname
host$ ls /etc/hostname /missing 2>&1 >/dev/null
ls: cannot access '/missing': No such file or directory
host$ ls -l /proc/$$/fd
lrwx------ 1 alice alice 64 Nov 28 11:02 0 -> /dev/pts/1
lrwx------ 1 alice alice 64 Nov 28 11:02 1 -> /dev/pts/1
lrwx------ 1 alice alice 64 Nov 28 11:02 2 -> /dev/pts/1
host$ tail -f /var/log/app.log | grep --line-buffered ERROR | tee errors.txt
host$ log(){ printf '%s %s\n' "$(date +%T)" "$*" >&2; }; log "starting"

Reading /proc/<pid>/fd

/proc/$$/fd shows where each descriptor of the current shell points: here, all three go to the terminal /dev/pts/1. For a running service, the same view shows which log files or sockets its descriptors are connected to.

Documented example, not captured output.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

How it works at runtime

stdin / stdout / stderr is part of Shell & Bash → stdin / stdout / stderr. stdin, stdout and stderr are conventional file descriptors 0, 1 and 2 inherited by Linux processes; they define data/control/error streams independently of whether a terminal, pipe, file or socket backs each descriptor.

Topic-specific state model. stdin, stdout and stderr are conventional file descriptors 0, 1 and 2 inherited by Linux processes; they define data/control/error streams independently of whether a terminal, pipe, file or socket backs each descriptor.

Interpretation rule. - stdout ≠ terminal output; it can point to pipe, file, socket or journal capture.

  • stderr text ≠ command failure and silence on stderr ≠ success.
  • isatty(stdout) differences ≠ cosmetic only; programs may change formatting, buffering or interactive behavior.
  • File descriptor number ≠ pathname; descriptor state is per-process and inherited/duplicated across exec.

Quick reference

Every process has three standard streams: stdin (fd 0), stdout (fd 1), stderr (fd 2). Programs should write data to stdout and diagnostics to stderr, so pipelines carry data while errors still reach the terminal or logs.

ls /nonexistent 1>out.txt 2>err.txt
exec 3> /tmp/trace.log; echo debug >&3; exec 3>&-     # custom fd
ls -l /proc/$$/fd

Under systemd, a service's stdout/stderr go to the journal by default (StandardOutput=journal), so echo in a service script ends up in journalctl -u.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Limits and how it fails

Primary failure point. Standard-stream evidence shows where bytes flow and how they are separated; it does not by itself establish application success or failure.

ReferenceConfiguration & state1 section · depth on demand

Configuration, persistence and lifecycle

Distribution/platform context. Terminal/TTY in Linux Fundamentals owns terminal endpoints; Pipes/Redirection own shell rewiring; this Topic owns standard descriptor roles and process-visible behavior.

Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor doclinux current

Authoritative/upstream documentation reviewed for bounded claims in Shell & Bash → stdin / stdout / stderr. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://www.gnu.org/software/bash/manual/bash.html · confidence 94%
Technical changelog & impact

What changed and what may be affected

Recent technical edits are separated from ordinary page timestamps. Relationship impact is advisory: it identifies nearby canonical references to review, not automatic breakage.

Related reference impactReview these references after material technical changes when their shared boundary is affected.
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
stdin stdout stderrlinux file descriptors 0 1 2standard streams linuxstderr stdout
Embedded reference data

Quick lookup tables

stdin / stdout / stderr operational reference

Topic-specific lookup boundaries, proof limits and next-step Evidence for stdin / stdout / stderr.

Lookup boundaryTopic-specific referenceOperational use
Scope / effective statestdin, stdout and stderr are conventional file descriptors 0, 1 and 2 inherited by Linux processes; they define data/control/error streams independently of whether a terminal, pipe, file or socket backs each descriptor.Establish exactly what this Topic owns before crossing into another subsystem.
Fast verificationPin host/distribution + shell/version/mode → identify cwd/mount namespace + input/path/stream → preserve quoting/expansion and final argv → map fd/pipeline/process boundaries → inspect object/variable/status/signal state → make one minimal reversible change → read back effective state → repeat original consumer/script/workload test.Use the shortest evidence path first; a fast check is a boundary test, not blanket proof.
Verification chainparent/session fd table → inherited fd 0/1/2 → program TTY/buffering behavior → read/write syscalls → backing terminal/pipe/file/socket → consumer + exit statusTrace requested/configured state through effective state to the observed workload result.
Failure boundaryStandard-stream evidence proves where bytes flow and how they are separated; it does not by itself establish application success or failure.Stop broad troubleshooting at the first boundary that is not supported by evidence.
Positive evidenceCorrelated Linux file/text/shell evidence can establish this proof path: parent/session fd table → inherited fd 0/1/2 → program TTY/buffering behavior → read/write syscalls → backing terminal/pipe/file/socket → consumer + exit statusUse this observation only for the specific claim it actually supports.
Evidence limitIt does not independently prove behavior outside the captured host, shell/session, cwd/namespace, input, process, filesystem, locale, time or workload scope. Standard-stream evidence proves where bytes flow and how they are separated; it does not by itself establish application success or failure.Do not promote a local success signal into end-to-end or adjacent-dependency proof.
Next troubleshooting checkContinue from the first unproven boundary: capture the exact shell-delivered argv/streams or resolved object, verify the effective target state, change only the demonstrated cause, then repeat the original consumer/script/workload and adjacent regression checks.Continue to the next unresolved boundary while preserving the original test condition.

stdin / stdout / stderr · operational reference

Compact Linux state, persistence, verification and boundary reference for fast lookup.

Reference boundarystdin / stdout / stderr
Topic pathShell & Bash → stdin / stdout / stderr
Runtime statestdin, stdout and stderr are conventional file descriptors 0, 1 and 2 inherited by Linux processes; they define data/control/error streams independently of whether a terminal, pipe, file or socket backs each descriptor.
Fast checks- Identify fd 0/1/2 targets in the same process/session and whether each endpoint is a TTY, pipe, file, socket or closed. - Capture data and diagnostics separately before parsing output programmatically. - Account for buffering and line/block behavior when chaining commands or services. - Verify the consuming process/service receives the expected stream and that status is checked independently of stderr content.
Verification chainparent/session fd table → inherited fd 0/1/2 → program TTY/buffering behavior → read/write syscalls → backing terminal/pipe/file/socket → consumer + exit status Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return/regression → explicit Evidence boundary. Boundary chain: local authoritative state → adjacent dependency → effective/programmed state → original workload/request → return path or regression → explicit Evidence boundary.
Failure boundaryStandard-stream evidence proves where bytes flow and how they are separated; it does not by itself establish application success or failure. Boundary chain: process/context → userspace configuration → kernel subsystem/resource → device/network/filesystem dependency → effective state → original workload.
Persistence ruleProve which configuration source owns the state, then prove that the running consumer loaded or generated the intended state.
Confirmation

Confirm action

Continue?