Reputation: 21
loopcount=1
loopmax=5
while [ $loopcount -le $loopmax ]
do
echo "loop iteration :$loopcount"
((loopcount=loopcount+1))
done
for this i am getting o/p like this
loop iteration :1
loop iteration :2
loop iteration :3
loop iteration :4
loop iteration :5
but if i change program ((loopcount=loopcount+1)) to (loopcount=loopcount+1) i am getting bellow output.
loop iteration :1
loop iteration :1
loop iteration :1
loop iteration :1
loop iteration :1
loop iteration :1
getting infinite times . what is difference of () and (()) ?
Upvotes: 0
Views: 224
Reputation: 70263
From man bash
:
(list)
list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of list.[...]
((expression))
The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent tolet "expression"
.
Upvotes: 2
Reputation: 14500
(...)
means to run the given command in a subshell. ((...))
means to do the arithmetic operation within the parens.
Note that a subshell cannot change the variables of the parent shell, so in your example you never update the value of loopcount in the parent. Also, in your single paren example, you would not be doing arithmetic, you'd be assigning the string loopcount+1
to the variable loopcount
so that if you did printf "%s\n" "$loopcount"
after that you would get the output loopcount+1
Upvotes: 2