On Thu, Oct 02, 2025 at 02:56:32PM +0200, Pourko via Bug reports for the GNU
Bourne Again SHell wrote:
> On Tue, 23 Sep 2025 05:07:18 +0200 (CEST), Pourko wrote:
> > On Thu, 18 Sep 2025 15:26:16 -0400 Chet Ramey wrote:
> > > There isn't a good way for a shell script to determine whether or not
> > > it's in the foreground or background, and whether it is in the same
> > > process group as the terminal.
> >
> > Attached is the patch that does it.
> > [...]
> > -s FILE True if file exists and is not empty.
> > -S FILE True if file is a socket.
> > -t FD True if FD is opened on a terminal.
> > + -T FD True if FD is opened on a terminal and is readable.
> > -u FILE True if the file is set-user-id.
> > -w FILE True if the file is writable by you.
> > -x FILE True if the file is executable by you.
> > [...]
Sorting of options is case sensitive.
> > 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?
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
Harder would be:
-W FD True if FD is opened on a terminal and is writeable.
--
Regards, Mike Jonkmans