The if [ 0 ] test checks if 0 an empty string (this is not the case), and returns true if it is not empty. The if [ 1 ] test similarly checks if 1 an empty string, and returns true if it is not empty. Similarly, the other two tests check if the true and false strings are empty ...
If you want to check the commands, run the commands:
if false; then echo False is true; else echo False is false; fi if true ; then echo True is true; else echo True is false; fi
Most machines do not have commands called 0 or 1 , so you cannot easily call them as commands.
You can also experiment with:
if sh -c "exit 1"; then echo Shell exited as true; else echo Shell exited as false; fi if sh -c "exit 0"; then echo Shell exited as true; else echo Shell exited as false; fi
In the 7th edition, Unix /bin/test (or perhaps /usr/bin/test ) was a test program, but usually you will find the link /bin/[ . When it was called with the name [ , it required that its last argument be ] so that you could write the condition in square brackets. (A Mac with macOS 10.14.6 in Mojave still has /bin/[ - it still works. On Linux systems, there is usually /usr/bin/[ , and it still works too.)
Soon after, the test operation was built into the shell, instead of being a separate executable, but the semantics remained basically unchanged. Since now they are different (built into the executable file), sometimes test operations have different functionality. POSIX defines the baseline; different shells and systems provide different extensions.
To date, the autoconf package recommends using test instead of [ , although this is mainly because it uses square brackets for another purpose.
Jonathan leffler
source share