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?
The format is to type the name, the equals sign = , and the value. Note there isn't a space before or after the equals sign. Giving a variable a value is often referred to as assigning a value to the variable.
Comparison Operators When comparing strings in Bash you can use the following operators: string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal. Use the = operator with the test [ command. Use the == operator with the [[ command for pattern matching.
== is a bash-specific alias for = and it performs a string (lexical) comparison instead of a numeric comparison. eq being a numeric comparison of course.
To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]
You can use expr
:
INPUT='flag'
LABEL=$(expr "${INPUT}" == "flag")
echo "$LABEL"
1
INPUT='flab'
LABEL=$(expr "${INPUT}" == "flag")
echo "$LABEL"
0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With