Bash Converting String to Boolean Variable

Bash Converting String to Boolean Variable

What's the best way to convert a literal string (e.g. "True" into the appropriate bash boolean variable). For instance java has java.lang.Boolean.valueOf(String)

Right now I'm using this in Bash:

if [ "${answers[2]}" = "TRUE" ] ; then
    clean=true; 
else 
    clean=false; 
fi

is there a way to do it and avoid the IF statement?

edit: to clarify its not by choice that I have String variable containing "TRUE" instead of just using a boolean variable. for full context this is the code

ans=$(yad --title='YadExample' --form --field=Opt1:CHK FALSE  --field=Opt2:CHK FALSE --field=Opt3:CHK TRUE);
#at this point the "yad" program is returning a string seperated by '|', e.g "TRUE|FALSE|TRUE"
IFS="|"
set -- $ans
answers=( $@ )
unset IFS
if [ "${answers[0]}" = "TRUE" ] ; then clean=true; else clean=false; fi
if [ "${answers[1]}" = "TRUE" ] ; then var2=true; else var2=false; fi
if [ "${answers[2]}" = "TRUE" ] ; then var3=true; else var3=false; fi
2

2 Answers

You could write a function to do the conversion for you:

function boolean() {
  case $1 in
    TRUE) echo true ;;
    FALSE) echo false ;;
    *) echo "Err: Unknown boolean value \"$1\"" 1>&2; exit 1 ;;
   esac
}

answers=(TRUE FALSE TRUE)
clean="$(boolean "${answers[0]}")"
var2="$(boolean "${answers[1]}")"
var3="$(boolean "${answers[2]}")"

echo $clean $var2 $var3

prints

true false true

Or, a little fancier:

function setBoolean() {
  local v
  if (( $# != 2 )); then
     echo "Err: setBoolean usage" 1>&2; exit 1 ;
  fi

  case "$2" in
    TRUE) v=true ;;
    FALSE) v=false ;;
    *) echo "Err: Unknown boolean value \"$2\"" 1>&2; exit 1 ;;
   esac

   eval $1=$v
}

answers=(TRUE FALSE TRUE)

setBoolean clean "${answers[0]}"
setBoolean var2 "${answers[1]}"
setBoolean var3 "${answers[2]}"

echo $clean $var2 $var3
4

You can just use something like:

[ "${answers[2]}" != "TRUE" ] ; clean=$?

You need to reverse the sense of the comparison if you want clean set to 1 on the condition being true.

Your sequence then becomes:

[ "${answers[0]}" != "TRUE" ] ; clean=$?
[ "${answers[1]}" != "TRUE" ] ; var2=$?
[ "${answers[2]}" != "TRUE" ] ; var3=$?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett
Author

Chloe Bennett

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.