Pcornel
Linux · Shell & Bash

stdin / stdout / stderr

Cheat sheet · https://pcornel.dk/topic.php?topic=2686

Back to the Topic

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.

Remember

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.

First 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

Commands

journalctl -u

Verify in this order

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

Where it usually breaks

First mismatch in persistent intent → provider/parser/generator → kernel/userspace runtime → dependency/security context → logs/counters → consumer-visible read-back; preserve identity/native diagnostic/pre-state before mutation.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=2686

Confirmation

Confirm action

Continue?