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

Viewed 32894

In bash script

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

Output: Yes

It represents that '1' is treated as true value.

But in code:

word = Linux
letter = nuxi
if echo "$word" | grep -q "$letter"
then
    echo "Yes"
else
    echo "No"
fi

Output: No

But echo "$word" | grep -q "$letter" will return 1, so why is the result is No.

How does the keyword if test the value returned by the command after if?

3 Answers
Related