[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Best way to expand a boolean into a string when true
|
From: |
Laurent Lyaudet |
|
Subject: |
Re: Best way to expand a boolean into a string when true |
|
Date: |
Mon, 2 Mar 2026 22:00:23 +0100 |
Le lun. 2 mars 2026 à 17:16, Stan Marsh <gazelle@xmission.com> a écrit :
>
> From: Laurent Lyaudet
> Subject: Re: Best way to expand a boolean into a string when true
> Date: Mon, 2 Mar 2026 16:34:14 +0100
>
> >Thanks for the link :).
> >I don't totally understand your code below:
>
>
> >I see that if varname ends with a ':' then it
> >will be substituted by 'x' if set and not null,
> >the test will succeed/return 0, and echo will choose the right
> >argument to output.
> >But I don't understand what does "${var+x}" in the other case.
> >There is no such substitution in the Bash manual.
>
> Each of the usual ${var<symbol>} things come in two flavors: with and
> without a preceding :. The difference is that with the :, they test
> for empty or unset, whereas without the :, they test only for unset.
>
> So:
> 1) ${foo+bar} is bar if foo is set (could be empty).
> 2) ${foo:+bar} is bar if foo is set and non-empty.
>
> ite() duplicates this functionality.
>
> =================================================================================
> Please do not send me replies to my posts on the list.
> I always read the replies via the web archive, so CC'ing to me is unnecessary.
>
> When responding to my posts, please try to refrain from giving bureaucratic
> answers.
> If you have nothing useful to say, then just click Next and go on.
>
Thanks :).
I had missed the following sentence in the manual:
"Omitting the colon results in a test only for a parameter that is unset."
I was reading with not enough care.
Moreover, I downloaded the HTML manual years ago,
and it doesn't give examples of "+" without the column.
Here is what I have:
-------------------------------------
${parameter:+word}
If parameter is null or unset, nothing is substituted, otherwise
the expansion of word is substituted.
$ var=123
$ echo ${var:+var is set and not null}
var is set and not null
---------------------------------------
It gives no example of ${foo+bar}.
It corresponds to this commit:
https://cgit.git.savannah.gnu.org/cgit/bash.git/commit/doc?id=74091dd4e8086db518b30df7f222691524469998
I saw that in the current version at:
https://www.gnu.org/software/bash/manual/bash.html
the examples are more verbose and include ${foo+bar}
---------------------------
${parameter:+word}
If parameter is null or unset, nothing is substituted, otherwise
the expansion of word is substituted. The value of parameter is not
used.
$ var=123
$ echo ${var:+var is set and not null}
var is set and not null
$ echo ${var+var is set}
var is set
$ var=
$ echo ${var:+var is set and not null}
$ echo ${var+var is set}
var is set
$ unset var
$ echo ${var+var is set}
$ echo ${var:+var is set and not null}
$
---------------------------------
It was added last year in this commit:
https://cgit.git.savannah.gnu.org/cgit/bash.git/commit/doc?id=b8c60bc9ca365f8261fa97900b6fa939f6ebc303
I updated my tab with bash manual this evening.
Next time, I should have less surprises.
Thanks :), best regards,
Laurent Lyaudet
- Best way to expand a boolean into a string when true, Laurent Lyaudet, 2026/03/01
- Re: Best way to expand a boolean into a string when true, Stan Marsh, 2026/03/01
- Re: Best way to expand a boolean into a string when true, Stan Marsh, 2026/03/01
- Re: Best way to expand a boolean into a string when true, Stan Marsh, 2026/03/02
- Re: Best way to expand a boolean into a string when true, Stan Marsh, 2026/03/02
- Re: Best way to expand a boolean into a string when true,
Laurent Lyaudet <=
- Re: Best way to expand a boolean into a string when true, Stan Marsh, 2026/03/03