In bash script
if [ 1 ]
then
echo "Yes"
else
echo "No"
fi
Yes
word = Linux
letter = nuxi
if echo "$word" | grep -q "$letter"
then
echo "Yes"
else
echo "No"
fi
echo "$word" | grep -q "$letter"
if
if
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'