Interview Prep/Shell Scripting/Arguments, I/O, here-docs, process substitution and subshells

What is `/dev/null`, `/dev/stdin`, `/dev/fd/N` and `/dev/tcp/host/port`?

IntermediateConceptArguments, I/O, here-docs, process substitution and subshells
Answer
  • /dev/null discards writes and returns EOF on reads.
  • /dev/stdin, /dev/stdout, /dev/stderr, /dev/fd/N refer to the process's file descriptors (handy for tools that only accept a filename).
  • /dev/tcp/HOST/PORT and /dev/udp/HOST/PORT are Bash pseudo-files that open a socket, useful when nc is not installed:
if timeout 3 bash -c '</dev/tcp/db.internal/5432' 2>/dev/null; then
  echo "postgres port open"
fi

/dev/tcp is a Bash feature (it may be disabled at compile time on some distros), not a real device.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arguments, I/O, here-docs, process substitution and subshells