ElenaD ElenaD - 1 year ago 111
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download