Detecting Standard Input in Bash

It’s often useful to know where your Bash script input is coming from.

  1. an active terminal,
  2. open file descriptors.

Interactive Terminal

[[ -t 0 ]] && echo 'terminal input'

Piped

[[ -p /dev/stdin ]] && echo 'piped input'

Redirected

[[ ! -t 0 && ! -p /dev/stdin ]] && echo 'redirected input'