How do I convert a string to a number? The following Bash script does not work as expected.
#!/bin/sh
mynum="0.02"
if [[ $mynum -lt 1 ]];then
echo "low"
else
echo "high"
fi
Error message
stack.sh: line 5: [[: 0.02: syntax error: invalid arithmetic operator (error token is “.02”)
The problem is that
bashnormally only supports integer arithmetic; you will need to punt floating or complex math todcorbc.You may be able to cheat in this case:
But this obviously isn’t generally applicable.