Re: Best way to expand a boolean into a string when true
Martin D Kealey <[email protected]> Tue, 3 Mar 2026 11:14:54 +1000
| Newsgroups | gmane.comp.shells.bash.bugs |
|---|---|
| Message-ID | <CAN_U6MX+zkxjEPE3oMd4o9XKF6Nh_Mb6-M0M_yETuAbGiNOrRw@mail.gmail.com> |
On Tue, 3 Mar 2026 at 01:34, Laurent Lyaudet <[email protected]> wrote in the [email protected] list: > But I don't understand what does "${var+x}" in the other case. > There is no such substitution in the Bash manual. I've seen many new Bash users struggle with this. It's in “man bash”, but functionally invisible to anyone who doesn't already know the answer. The answer is hidden in plain view, in the paragraph *before* the list of possible expansions: When not performing substring expansion, using the forms documented below (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 *unset*. ${parameter:-word} Use Default Values. If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted. ${parameter:=word} Assign Default Values. If parameter is unset or null, the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way. ${parameter:?word} Display Error if Null or Unset. If parameter is null or unset, the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted. ${parameter:+word} Use Alternate Value. If parameter is null or unset, nothing is substituted, otherwise the expansion of word is substituted. This wording made some sense in 1987 when those four were the only modifiers, but now they're merely 4 among at least 13, and the colon rule does not even apply to any of the others. Such preface text is not just unhelpful, it's harmful. When you know it's there, it gives the impression that the rule is clear, but when you don't already know it's there, it's practically invisible. Someone searching in the manual for this specific topic will start focussed reading from a point where that text may not even be visible in their terminal. I suggest that the preface text be removed, and the four following paragraphs be amended to read something like: ${parameter-word} or ${parameter:-word} Use Default Values. If parameter is unset (or set but null, if the colon is included), the expansion of word is substituted. Otherwise, the value of parameter is substituted. ${parameter=word} or ${parameter:=word} Assign Default Values. If parameter is unset (or set but null, if the colon is included), the expansion of word is assigned to parameter. The value of parameter is then substituted. Positional parameters and special parameters may not be assigned to in this way. ${parameter?word} or ${parameter:?word} Display Error if Null or Unset. If parameter is unset (or set but null, if the colon is included), the expansion of word (or a message to that effect if word is not present) is written to the standard error and the shell, if it is not interactive, exits. Otherwise, the value of parameter is substituted. ${parameter+word} or ${parameter:+word} Use Replacement Value. If parameter is unset (or set but null, if the colon is included), nothing is substituted, otherwise the expansion of word is substituted. Alternatively, remove the preamble paragraph and add this paragraph as the 5th entry in the list: ${parameter-word} ${parameter=word} ${parameter?word} ${parameter+word} These function the same as the corresponding colon variants, except that they check only for the parameter being *unset*; being set but null is treated the same as being set to any other value. -Martin PS: Whilst the English meaning of “null” can be “empty”, in computing it's commonly used to mean “absence of an object” as distinct from “empty object”, so I would avoid, but I would leave that as a change for another day.