ElenaD ElenaD - 1 year ago 65
Bash Question

Bash error: "line 8: [: 2: unary operator expected"

Please help me with this error I received with a shell script in Bash:

line 8: [: 2: unary operator expected


echo "Input your number for factorial calculation: "
read $nr


while [ $counter -le $nr ]
factorial=$(( $factorial * $counter ))
counter=$(( $counter + 1 ))

echo "The result " $nr "! is:"
echo $factorial

Something is wrong with the while line. Maybe the
is not used correctly?

Answer Source

read takes name of the variable, not its value. You need to replace read $nr with read nr.