Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: detect pipe



On Wed, Jan 27, 2021 at 8:45 AM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> I have a function that as used this test for piping quite reliably:
>
>      if [ ! -t 0 ]; then
>
> ... but today it stopped working.

[ -t 0 ] or [[ -t 0 ]] mean that the standard input is a terminal.
It's quite possible for standard input to be neither a terminal (so [[
! -t 0 ]] is true) nor a pipe.  For example, redirection from a file
or use of a here-document will both take the then-branch of "if [[ !
-t 0 ]]; then ... else ... fi".

> there a better way?  This is probably not strictly a zsh problem but the
> randomness of it working intermittently suggests some time lag or delay

A race condition would be unlikely to be a cause here.

[[ -p /dev/fd/0 ]] would be a more reliable test, because of the way
conditional expressions special case the prefix "/dev/fd/".




Messages sorted by: Reverse Date, Date, Thread, Author