When we want to check whether current terminal is attached to stdin, stdout and stderr streams or not, we can use the below codes. I tried with piping, and redirection so both works well.
As seen in below 6 usages, stdin is not listed in output of commands except first one. Because stdin is used by piping or redirection.
$ ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; echo hello)
stdin
stdout
stderr
hello
$ echo hello | ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat)
stdout
stderr
hello
$ ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat) <<<"hello"
stdout
stderr
hello
$ ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat) <<<$(echo hello)
stdout
stderr
hello
$ ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat) < foo.txt
stdout
stderr
Hello World
$ ([ -t 0 ] && echo stdin; [ -t 1 ] && echo stdout; [ -t 2 ] && echo stderr; cat) < <(cat foo.txt)
stdout
stderr
Hello World
Check current tty
TTY is a teletypewriter which is a terminal used from 1840. This word is still used today as virtual terminal.
$ tty
/dev/ttys000
List tty
$ who
kenanhancer console Jun 30 16:25
kenanhancer ttys000 Jul 11 02:25
$ w
2:36 up 10 days, 10:12, 2 users, load averages: 1.60 1.50 1.54
USER TTY FROM LOGIN@ IDLE WHAT
kenanhancer console - 30Jun22 10days -
kenanhancer s000 - 2:25 - w
$ ps
PID TTY TIME CMD
91820 ttys000 0:00.10 -bash
88521 ttys002 0:00.04 /usr/local/bin/bash -l
$ ls -l /dev/fd
total 0
crw--w---- 1 kenanhancer tty 0x10000000 11 Jul 02:38 0
crw--w---- 1 kenanhancer tty 0x10000000 11 Jul 02:38 1
crw--w---- 1 kenanhancer tty 0x10000000 11 Jul 02:38 2
drw-r--r-- 67 kenanhancer staff 2144 11 Jul 02:16 3
dr--r--r-- 1 root wheel 0 30 Jun 16:24 4