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

前端 未结 3 1508
没有蜡笔的小新
没有蜡笔的小新 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

    The return value of a command is checked. [ 1 ] has a return value of 0 (true). Any other return value (like 1) indicates an error.

    You can display the return value of the last executed command using the $? variable:

    true
    echo $?
    # returned 0
    false
    echo $?
    # returned 1
    echo $?
    # returned 0 as the last executed command is 'echo', and not 'false'
    

提交回复
热议问题