I m writing shell program to compare two numbers and show the status using test command
#!/bin/sh
a="42"
b="23"
echo $a
test [$a -eq $b]
echo $?
but I m getting error like this eq: line 5: test: [42: integer expression expected 2
I m writing shell program to compare two numbers and show the status using test command
#!/bin/sh
a="42"
b="23"
echo $a
test [$a -eq $b]
echo $?
but I m getting error like this eq: line 5: test: [42: integer expression expected 2
You have to change the line
test [$a -eq $b]
to
test $a -eq $b
test expects an integer expression and [$a -eq $b] is not an integer expression.
Please remove the square brackets.
From the manual pages, you use the square brackets as an alternate to explicitly using the "test" word. So it is one or the other.
From the man page:
test Evaluate a conditional expression expr. Syntax
test expr
[ expr ]
[[ expr ]] [ is a synonym for test but requires a final argument of ].
Try man test and in the manual page you will see that it expects your expression after test. Your expression being $a -eq $b, the command should be test $a -eq $b
What you are doing wrong:
man [ and you will be directed to the same manual page as man test. You are trying to use both[ itself is a command, you need a space after [ before the next part of the command. Therefore it should be [ $a -eq $b ] instead of [$a -eq $b]In the line where you have used test [$a -eq $b], you can use either
test $a -eq $b or[ $a -eq $b ]