$ s2=5 s1=4 $ echo $s2 $s1 5 4 $ res= expr $s2 - $s1 1 $ echo $res
What actually happens on the fourth line is that res is not set and exported to the expr command. Thus, when you run [ "$res" -lt 0 ] res expands to zero, and you see an error.
You can simply use an arithmetic expression :
$ (( res=s2-s1 )) $ echo $res 1
The arithmetic context ensures that the result will be integer, so even if all your terms are undefined, you will get an integer result (namely zero).
$ (( res = whoknows - whocares )); echo $res 0
Alternatively, you can tell the shell that res is an integer by declaring it as such:
$ declare -i res $ res=s2-s1
Interestingly, the right-hand side of the job is processed in an arithmetic context, so you don't need $ for extensions.
source share