Test command with special character not work

Hi all,

Case 1 :

A=88^M
[ $A -eq 88 ] && echo "PASS"

Result:

PASS

Case 2:

A=88
[ $A -eq 88 ] && echo "PASS"

Result:

PASS

I would like to know why Case 1 and Case 2 got the same result? What make ^M ignored ?

Thanks in advance.

Enclose them with simple quote and test again

1 Like

Thanks the quick reply.

Case 1 :
A=88^M
[ $A -eq '88' ] && echo "PASS"

Result:
PASS

Case 2:
A=88
[ $A -eq '88' ] && echo "PASS"

I have retest with single quote and got the same result.

As I am facing a problem that, the script work in some machine and not work in another, but the script is exactly the same. So, I would like to know any terminal setting will affect the test command when the string comparison with special character.

Other way around.... quotes around the variable, not the number :smiley:

As well as on what is on the right side of the = sign :smiley:

A='88^M'

[ "$A" ...

O ... Thanks ... But it got the Syntax error.

Using "==" rather than -eq will get the difference result. However, I would like to know the reason that what make the -eq comparison ignored special character.

Thanks again.

-eq is a numerical comparison. I get a syntax error for your first case on bash/cygwin, but it may be that your shell implementation is just ignoring any trailing characters which don't evaluate to a number.