Search code examples
bashstring-comparison

In bash, how to make a comparison and assign the boolean result to a variable


I am doing a string comparison between a variable and a constant. The result of the comparison, either true or false, is assigned to another variable.

LABEL=$("${INPUT}" == "flag");

However, I am failing. Any suggestion?


Solution

  • You can use expr:

    INPUT='flag'
    LABEL=$(expr "${INPUT}" == "flag")
    echo "$LABEL"
    1
    
    INPUT='flab'
    LABEL=$(expr "${INPUT}" == "flag")
    echo "$LABEL"
    0