[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Weird set -u error



Hi,

> $ cat Makefile.test
> SHELL := /bin/bash -u
> [...]
> /etc/bash.bashrc: line 7: PS1: unbound variable
> [...]
> Why do I get that unbound variable error?

According to a comment in /etc/bash.bashrc it tests for emptiness of PS1
in order to detect non-interactive shell sessions:

  # If not running interactively, don't do anything
  [ -z "$PS1" ] && return

See
  https://sources.debian.org/src/bash/5.2~rc2-2/debian/etc.bash.bashrc/#L6


It should rather test for the variable being set, rather than being empty.

Jumping ahead of more skilled experts i found in the web
  https://stackoverflow.com/questions/3601515/how-to-check-if-a-variable-is-set-in-bash
which brings me to a proposal for /etc/bash.bashrc

  [ -z "${PS1:+x}" ] && return

Testing it:

  $ bash -u

reports various "unbound variable".
Now test whether a non-empty PS1 is properly recognized

  $ [ -z "${PS1:+x}" ] && echo "empty"
  $

Now with undefined PS1:

  $ unset PS1

makes the prompt invisible, but

  [ -z "${PS1:+x}" ] && echo "empty"
  empty

and

  set PS1=
  [ -z "${PS1:+x}" ] && echo "empty"
  empty

Back to a visible prompt:

  PS1='$ '
  $ [ -z "${PS1:+x}" ] && echo "empty"
  $


Have a nice day :)

Thomas


Reply to: