[ $Server_Name=1 ]
does not work as intended because the syntax inside the single brackets isn't special to Bash. As usual, the variable $Server_Name
gets substituted by 1, so all the test ([
) command sees is a single argument: the string 1=1
. Since that sting has a non-zero length, test returns true.
For POSIX-compliant shells, you can use the following test commands:
[ "$Server_Name" = 1 ]
checks is the $Server_Name
is equal to the string 1
.
[ "$Server_Name" -eq 1 ]
checks is the $Server_Name
is equal to the number 1
, i.e., it does a numeric comparison instead of a string comparison.
The return value of the two command will differ, e.g., if you define Server_Name=01
. The first one will return false, the second will return true.
Note that if the possibility exists that the variable $Server_Name
is undefined, it must be quoted or test will display an error when invoked.
As David points out below, you must use "-eq" to test numeric values. You might also want to check for a blank variable to avoid errors;
if [ ! "x$var" = "x" ]; then\n if [ $var -eq 1 ]; then ...
– mikebabcock – 2013-12-16T01:41:35.640