Hi,

maybe i am just too dumb to read the manual, or maybe i made an
archeological discovery in the shells we use.

I came to a strange shell gesture which i find neither in man bash nor
man dash. Nevertheless it works the same in both shells.

GRUB's test scripts often show this gesture

  : "${TMPDIR=/tmp}"

Well known and described is
  ${X:=Y}
which assigns a default value to variable X if it is empty (man bash
says: "If [...] unset or null").
Also known and described is ":", the no-op command.
So
  : "${TMPDIR:=/tmp}"
would not be a riddle. But "=" instead ":=" causes a subtle change in
behavior. A defined but empty variable does not get the default content:

  $ unset x
  $ : ${x=YYY}
  $ echo $x
  YYY
  $ x=
  $ : ${x=YYY}
  $ echo $x

  $

With ":=" the empty variable gets a new stuffing:

  $ : ${x:=YYY}
  $ echo $x
  YYY
  $

The dash shell instead of bash shows the same behavior.

Is this legacy behavior ?
Are there shells which do ${X=Y} but not ${X:=Y} ?

--------------------------------------------------------------------
Reason why i ask:

I came to that gesture while trying to find out why a run of GRUB's
"make check" as superuser left a lot of empty directories in the
/-directory, Most GRUB tests do early

  : "${TMPDIR=/tmp}"

or use variable expansion with default value: ${TMPDIR:-/tmp}
But tests/grub_cmd_cryptomount uses TMPDIR naively so that its test
directories end up as
  /1718898505.LUKS1_test_cryptsetup_defaults
  ...
  
/1718912880.LUKS2_test_with_second_key_slot_and_first_slot_using_different_password

I would like to talk GRUB into using ${X:=Y} generally, because it does
not matter whether TMPDIR is defined empty or is unset.
So i need background knowledge about the currently used ${X=Y}.

(One may say that is not a good idea to run a test as superuser.
But most GRUB tests create and mount filesystems. So after a change in
the tool grub-fstest i had to do it as superuser to check for new test
failures caused by my change. It was only one and that was expected.)


Have a nice day :)

Thomas

Reply via email to