Out of curiosity, when doing a bash variable comparison (its value being an integer
) it's possible to test it against some predefined value either declared as an int
or as a string
.
Sample script:
#!/bin/bash
f1()
{
[ "$1" == "1" ] && echo "$FUNCNAME: \"1\" compared as string"
}
f2()
{
[[ "$1" -eq 1 ]] && echo "$FUNCNAME: \"1\" compared as int"
}
f1 $1
f2 $1
Output:
$ ./param.sh 1
f1: "1" compared as string
f2: "1" compared as int
and
$ ./param.sh blah
$
Both functions behave the same way, and so I'm wondering if there's a preferred way when checking an integer variable?
I would go for checking int
versus int
as it's more strict but I wonder if there are any draw backs doing it with string
?
In this case, f2()
is also more strict about the comparison, i.e. passing a decimal value will break it, whereas f1()
will take it no problem.