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?
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?
You can use expr
:
INPUT='flag'
LABEL=$(expr "${INPUT}" == "flag")
echo "$LABEL"
1
INPUT='flab'
LABEL=$(expr "${INPUT}" == "flag")
echo "$LABEL"
0
This is probably easier and can cover more test cases. For you can get more details about string comparisons and test cases via 'man test'. Here's a pretty basic sample.
if [ "${INPUT}" == "flag" ]; then
LABEL=${INPUT}
fi
echo ${LABEL}