Parsing boolean string statements in bash

Revision history
Tags: bash

Preface

While writing a custom GitHub Action with a Docker entrypoint written in bash, I want the users to be able to pass boolean variables as true and false, yes and no, 1 and 0, and en empty string being considered false.

Using a function with a regular expression

I first went with return 0 and return 1 in the function, relying on the function’s exit code, but I felt this increased my cognitive load due to 0 being success, i.e. true, and 1 being false. I found that instead echoing a string gives for easier reading.

# Returns a string `true` if the string is considered a boolean true,
# otherwise `false`. An empty value is considered false.
function str_bool {
  local str="${1:-false}"
  local pat='^(true|1|yes)$'
  if [[ "$str" =~ $pat ]]
  then
    echo 'true'
  else
    echo 'false'
  fi
}

I can now configure my variables as booleans!

enable_debug_logging=$(str_bool "${ENABLE_DEBUG_LOGGING:-}")

if [ "$enable_debug_logging" = "true" ]
then
  # ...
  echo "Debug logging enabled!"
fi

If you have any comments or feedback, please send me an e-mail. (stig at stigok dotcom).

Did you find any typos, incorrect information, or have something to add? Then please propose a change to this post.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.