If conditional statement in Bash reports an error [: missing `] ‘

This is my little demo


read -p "Please input three numbers:" n1 n2 n3
if [ $n1 -gt $n2 ] && [ $n1 -gt $n3 ]
echo "max_num is $n1"
elif [ $n3 -gt $n2 ] && [ $n1 -lt $n3 ]
echo "max_num is $n3"
echo "max_num is $n2"

The error [: missing `]] is reported mainly for the following reasons:

Spaces shall be used between if and ‘[‘, ‘[‘ and condition, ‘]’ and condition, ‘-GT’ and the number to be compared before and after;

When using relational operators’ && ‘ or ‘ | ‘in the middle, be sure to enclose the conditions before and after with square brackets.

Similar Posts: