Ideally, this would be scriptable in the shell, but Perl or Python would be fine. The C code may be useful, but probably does not cope with the costs / benefits.
I understand that redirecting to a FIFO (named pipe) can be indistinguishable from a real pipe, and thatβs enough for me to really care.
Optimal POSIX solutions are best suited for UNIX / Linux, whatever the option, but at least I need something that works on Darwin (MacOS X).
Before writing my answer - I already know about test -t - this will tell me whether stdout is a terminal (in which case it is definitely not a pipe, but it will not tell me whether stdout was redirected to a file, a nonterminal character device or a UNIX socket -domain, not per channel.
Estimated use case: I have a command that needs to be run inside backquotes in a shell so that it can output commands that set environment variables. I would like the command to interrupt with an error if stdout is not redirected to the channel, since in this case it was definitely not called by eval `mycommand`; .
If there is a special environment variable that the shell will set when running the command in backquotes, which would be useful, but since it is likely to be specific to bash or zsh or something more important, channel detection is more important.
source share