谁能在控制结构中解释这个?
can anyone explain this while control structure?
#!/bin/bash
number=0
while [ "$number" -lt 10 ]
do
echo -n "$number"
((number +=1))
done
echo
谁能解释为什么 ((number += 1))
中不需要 $
?
当您将 shell 算术与 (( ... ))
一起使用时,变量前的 $
是可选的。由于这仅用于算术,因此不允许使用字符串,因此任何不是数字或运算符的未加引号的标记都被视为变量。 Bash 手册中关于 Shell Arithmetic 的部分解释说:
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.
#!/bin/bash
number=0
while [ "$number" -lt 10 ]
do
echo -n "$number"
((number +=1))
done
echo
谁能解释为什么 ((number += 1))
中不需要 $
?
当您将 shell 算术与 (( ... ))
一起使用时,变量前的 $
是可选的。由于这仅用于算术,因此不允许使用字符串,因此任何不是数字或运算符的未加引号的标记都被视为变量。 Bash 手册中关于 Shell Arithmetic 的部分解释说:
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.