On Fri, Oct 03, 2025 at 08:51:02PM -0400, Grisha Levit wrote: > On Thu, Oct 2, 2025 at 5:48 PM Mike Jonkmans <[email protected]> wrote: > > On Thu, Oct 02, 2025 at 02:56:32PM +0200, Pourko via Bug reports for the > > GNU Bourne Again SHell wrote: > > > > 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
Good catch. Implementation shining through: bash's read influenced by system's read. Doesn't have to be though. It's a shame that POSIX caters for non-free os. Maybe it is time for FOSSIX. > > > On Tue, 23 Sep 2025 05:07:18 +0200 (CEST), Pourko wrote: > > > > Now... > > > > [ -t 0 ] && [ ! -T 0 ] > > > > ...means we are running in the background. > > > > > > It puzzles me that I seem to be the only one excited by this. > > > Don't you find it to be a useful thing to have? > > 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. Luckily, the -n option is not specified by POSIX. -- Regards, Mike Jonkmans
