help-bash
[Top][All Lists]
Advanced

[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: Sun, 1 Mar 2026 16:49:21 +0100

Le dim. 1 mars 2026 à 16:35, Laurent Lyaudet
<laurent.lyaudet@gmail.com> a écrit :
>
> Hello,
>
> Assume I have a variable that simulates a boolean:
>   declare -i LFBFL_verbose
> and I want to substitute either the empty string if it is 0,
> or the string "--verbose" if it is 1.
> What's the best way to do this, please ?
>
> Here is what I tried:
>   LFBFL_verbose=1
>   echo "${LFBFL_verbose/1/--verbose}"
>   # works
>   LFBFL_verbose=0
>   echo "${LFBFL_verbose/1/--verbose}"
>   # doesn't work since it prints "0"
>
> or
>   echo "${${LFBFL_verbose/0/}:+--verbose}"
>   # bad substitution
>
> I miss ternary operator :)
> echo LFBFL_verbose ? "--verbose" : ""
>
> Thanks, best regards,
>      Laurent Lyaudet

Sorry for the wrong subject of the email.
It should have been:
Best way to substitute a string to a boolean true

Note that:
echo "${${LFBFL_verbose/0/}/1/--verbose}"
also yields "bad substitution".
So a one-liner for that seems not obvious.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]