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
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 4 '<=' 6 1
Two things to note:
testreturns 0 for true (which is the standard for exit codes), but
exprprints 1 for true.
You may type in the following command:
echo $(test -e myFile.txt) $?