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

This is my little demo

#!/bin/bash

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

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: