In a pure POSIX shell,
if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi
returns “terminal”, because the output is sent to your terminal, whereas
(if [ -t 1 ] ; then echo terminal; else echo "not a terminal"; fi) | cat
returns “not a terminal”, because the output of the parenthetic element is piped to cat
.
The -t
flag is described in man pages as
-t fd True if file descriptor fd is open and refers to a terminal.
… where fd
can be one of the usual file descriptor assignments:
- 0: standard input
- 1: standard output
- 2: standard error