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

How do you check whether a script is running interactively or with a terminal?

IntermediateConceptArguments, I/O, here-docs, process substitution and subshells
Answer
[[ -t 0 ]] && echo "stdin is a terminal"
[[ -t 1 ]] && echo "stdout is a terminal (colours OK)"
[[ $- == *i* ]] && echo "interactive shell"

if [[ -t 1 ]]; then RED=$'\e[31m' RESET=$'\e[0m'; else RED='' RESET=''; fi

Disable colours and progress bars when stdout is not a terminal, so logs in CI and journald stay clean. Honour NO_COLOR if set.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

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