On Wed, Jul 24, 2024 at 15:33:11 +0200, Thomas Schmitt wrote:
> 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:


$ man bash
[...]
   Parameter Expansion
   [...]
       When not performing substring expansion, using the forms documented be‐
       low  (e.g.,  :-),  bash  tests  for  a parameter that is unset or null.
       Omitting the colon results in a test only for a parameter that  is  un‐
       set.

       ${parameter:-word}
              Use  Default  Values. [...]


Everyone skips over the sentence that begins with "Omitting the colon".

Every time we try to tell Chet, "Hey, man, please add examples that
show BOTH syntaxes", he blows us off, because this is the way POSIX
documents it.  If it's good enough for POSIX, then it must be good
enough for bash, right?[1]

Ordinary users search for the SYNTAX.  Then when they find a list of
examples, they only read that list.  They don't read the text BEFORE
the list.  They might read text AFTER the list, but even that's rare.

This is why I created <https://mywiki.wooledge.org/BashSyntaxReference>.
You can look up syntax there and see what it does.


[1]Hmm, it looks like POSIX added a table:
   
<https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_06_02>
   actually shows both forms, if you scroll down a bit.

Reply via email to