Srikanth Power
Srikanth Power

Reputation: 170

can anyone explain this while control structure?

#!/bin/bash
number=0
while [ "$number" -lt 10 ]
do
    echo -n "$number"
    ((number +=1))
done
echo

Can anyone explain why $ is not needed in ((number += 1))?

Upvotes: 0

Views: 34

Answers (1)

Barmar
Barmar

Reputation: 782166

When you use shell arithmetic with (( ... )), the $ before variables is optional. Since this is just used for arithmetic, strings are not allowed, so any unquoted token that isn't a number or operator is treated as a variable. The section of the Bash Manual on Shell Arithmetic explains:

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax.

Upvotes: 2

Related Questions