How does the keyword “if” test if a value is true or false?

前端 未结 3 1448
没有蜡笔的小新
没有蜡笔的小新 2021-02-04 06:33

In bash script

if [ 1 ]
then
   echo \"Yes\"
else
   echo \"No\"
fi

Output: Yes

It

3条回答
  •  再見小時候
    2021-02-04 06:58

    This is because the grep failed to find the $letter in $word, hence the exit code is 1. Whenever a process in linux return a code other than 0 then it means it failed. 0 means exited successfully. You can verify this by echo "Linux" | grep -d "nuxi"; echo $?

    On the other hand in scripting world 0 means false and 1 mean true. So the grep failed to find the word and send 1 as an exit code to if, which took it as a true value.

提交回复
热议问题