Best way to expand a boolean into a string when true
From:
Laurent Lyaudet
Subject:
Best way to expand a boolean into a string when true
Date:
Sun, 1 Mar 2026 16:35:51 +0100
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