On Fri, 26 Aug 2022 at 18:02, <to...@tuxteam.de> wrote:
> On Fri, Aug 26, 2022 at 08:17:10AM +0100, Tim Woodall wrote:

> > $ cat Makefile.test
> > SHELL := /bin/bash -u
> > MAKEFLAGS += --no-builtin-rules
> > .SUFFIXES:
> >
> > all:
> >         echo done
> > $ make -f Makefile.test
> > echo done
> > done
> > $ make -f Makefile.test | cat
> > echo done
> > /etc/bash.bashrc: line 7: PS1: unbound variable
>                     ^^^^^^^^^^^
> > Why do I get that unbound variable error? (I know how to fix it, I just
> > don't understand why it only happens when I pipe the output)

Hi, I'm no expert, but I would explain this as follows.

> > $ make -f Makefile.test

In the above command, make runs an interactive shell, because its standard
output is connected to a terminal.

> > $ make -f Makefile.test | cat

Whereas in the above command, make runs a non-interactive shell, because
it is in a pipeline so its standard output is not connected to a terminal,
but rather to the standard input of the 'cat' command.

PS1 is unbound in the second case because when the shell starts in
non-interactive mode, it does not set PS1.

The behaviour of interactive shells is explained in the three pages
linked here:
  https://www.gnu.org/software/bash/manual/html_node/Interactive-Shells.html

You can find expert responses on this topic by asking the mailing list:
  https://lists.gnu.org/mailman/listinfo/help-bash

>   [ -z "${PS1:-}" ] && return
>
> But I'll defer to more shell-savvy people for a proper fix.

Regarding the side discussion of how to test whether a bash shell is
interactive, both bash and dash shells provide the 'i' flag inside the
special variable $- (hyphen) for precisely this purpose.

So I would be inclinded to use that, rather than the old way of examining
$PS1. Unlikely, but possible, that some bizarre user has unset their $PS1
variable.

Some bash project documentation on this topic is here:
  
https://www.gnu.org/software/bash/manual/html_node/Is-this-Shell-Interactive_003f.html

Reply via email to