When I type this in the terminal
test 4 -lt 6
I don't get any output. Why not? I need that 0 or 1
When I type this in the terminal
test 4 -lt 6
I don't get any output. Why not? I need that 0 or 1
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
$? variable -- at least until it gets overwritten by the next command you execute.
$PIPESTATUS array to get the result of multiple commands in a pipeline. $? will by be the result of the last command in the pipeline if the pipefail option is off (the default).
if test 4 -lt 6; then echo test succeeeded; else echo test failed; fi
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:
test. test returns 0 for true (which is the standard for exit codes), but expr prints 1 for true.test shell builtin, which is considerably faster (about 50 times on my machine) than the test and expr executables from the coreutils package.
test a builtin or program (on superuser.com)