On Fri, 3 Oct 2025 20:51:02 -0400, Grisha Levit wrote: > On Thu, Oct 2, 2025 at 5:48 PM Mike Jonkmans <[email protected]> wrote: > > > > It is not a common case. > > > > Can't that check be done with something like: > > trap '' SIGTTIN > > if read -rd '' -n 0; then > > echo Foreground > > else > > echo Background > > fi > > trap SIGTTIN > > Unfortunately, this relies on non-portable behavior of read(2) with a > size argument of 0. Error detection is not required in this case per > POSIX [1] and, indeed, on macOS `read -rd '' -n 0' returns 0 when called > in a background job. > > [1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/read.html > > > > On Tue, 23 Sep 2025 05:07:18 +0200 (CEST), Pourko wrote: > > > > Now... > > > > [ -t 0 ] && [ ! -T 0 ] > > > > ...means we are running in the background. > > I wonder if the `tcgetpgrp((fd) == getpgid(0)' test you propose wouldn't > be more appropriate for `read -n0' (or some other option combination) to > do rather than have it as a `test' operation.
I went with "test -T 0" because to me it seems the easiest, and the most intuitive. I mean, (judging from me personally), the average user won't know how to dive into the quirks of read -n0, let alone even think about SIGTTIN.
