Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why if [ false ]; then echo 'ok'; fi; prints ok?

Tags:

linux

bash

Why when I type in bash: if [ false ]; then echo 'ok'; fi; I get ok string as a result? The similiar result I can get also when using variable: ok=false; if [ $ok ]; then echo 'ok'; fi;

like image 669
Adam Sznajder Avatar asked Nov 08 '12 17:11

Adam Sznajder


1 Answers

if [ false ] is equivalent to if [ -n "false" ] - it's testing the length of the string. If you are trying to test the exit code of /bin/false, use if false (no [, which for many, but not all, modern shells is a shell builtin roughly equivalent to /usr/bin/[ or /usr/bin/test).

like image 138
twalberg Avatar answered Nov 09 '22 14:11

twalberg