bashshellif-statement

In bash, why does if [ 1 ] not fail?


In shell scripting (bash) why does the conditional get satisfied

if [ 1 ]
then
 echo should not enter
fi

#Output
should not enter

Solution

  • The square brackets in bash are equivalent to calling test with the arguments within the brackets. man test says that

    STRING equivalent to -n STRING

    and

    -n STRING the length of STRING is nonzero

    , and 1 is not an empty string. if [ 0 ], if [ false ] and if [ no ] have the same result, but if [ "" ] has not.