Test command in unix doesn't print an output
You get 0 or 1. In the exitcode.
bash-4.2$ test 4 -lt 6
bash-4.2$ echo $?
0
bash-4.2$ test 4 -gt 6
bash-4.2$ echo $?
1
Update: To store the exitcode for later use, just assign it to a variable:
bash-4.2$ test 4 -lt 6
bash-4.2$ first=$?
bash-4.2$ test 4 -gt 6
bash-4.2$ second=$?
bash-4.2$ echo "first test gave $first and the second $second"
first test gave 0 and the second 1
Another way is
test 4 -lt 6 && echo 1 || echo 0
But be careful in that case. If test
returns success and echo 1
fails echo 0
will be executed.
If you want the result of a comparison on standard out instead of an exit code, you can use the expr(1)
command:
$ expr 4 '<=' 6
1
Two things to note:
- you will likely need to quote the operator as a lot of them conflict with shell metacharacters
- the output value is the opposite of the return code for
test
.test
returns 0 for true (which is the standard for exit codes), butexpr
prints 1 for true.