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.
More on Arguments, I/O, here-docs, process substitution and subshells
How does `getopts` work? Show a complete example.IntermediateWhat is the difference between `getopts` and `getopt`?AdvancedExplain file descriptors 0, 1 and 2 and the main redirection operators.BeginnerWhy is `cmd 2>&1 > file` different from `cmd > file 2>&1`?AdvancedWhat is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?BeginnerWhat is a here-string (`<<<`)?Intermediate