Re: Best way to expand a boolean into a string when true
From:
Stan Marsh
Subject:
Re: Best way to expand a boolean into a string when true
Date:
Sun, 01 Mar 2026 11:20:13 -0700
From: Laurent Lyaudet
Subject: Best way to expand a boolean into a string when true
Date: Sun, 1 Mar 2026 16:35:51 +0100
>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
One way to get this is redefine your problem spec slightly, so that
your LFBFL_verbose variable is unset (or empty) rather than 0 to mean
"no". Any value (other than empty/null) means "yes".
Then you can do:
$ echo ${LFBFL_verbose:+--verbose}
=================================================================================
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.