In bash/ksh/dash:
$ input_redirected() { [[ -t 0 ]] && echo input is not redirected || echo input is redirected; }
$ input_redirected foo
input is not redirected
$ input_redirected < /dev/null
input is redirected
$ cat /etc/passwd | input_redirected
input is redirected
in fish
$ function input_redirected; test -t 0; and echo not redirected; or echo redirected; end
$ input_redirected foo
not redirected
$ input_redirected < /dev/null
not redirected
$ cat /etc/passwd | input_redirected
not redirected
However, using isatty 0
instead of test -t 0
does provide the expected output.
What is test -t FD
test doing in fish that it does not behave the same as POSIX-type shells?
It's a bug: https://github.com/fish-shell/fish-shell/issues/1228
The workaround is to use command test
, which is also what isatty
does - see type isatty
.