Why ((count ++)) returns the first exit code for the first time

I do not know why the line below returns 1, and subsequent executions ((count++)) return 0.

 [ me@server ~]$ count=0 [ me@server ~]$ echo $? 0 [ me@server ~]$ count++ -bash: count++: command not found [ me@server ~]$ (count++) -bash: count++: command not found [ me@server ~]$ ((count++)) [ me@server ~]$ echo $? 1 <------THIS WHY IS IT 1 AND NOT 0?? [ me@server ~]$ ((count++)) [ me@server ~]$ echo $? 0 [ me@server ~]$ ((count++)) [ me@server ~]$ echo $? 0 [ me@server ~]$ echo $count 3 
+6
source share
1 answer

See excerpt on the help let page,

If the last ARG evaluates to 0, let it return 1; 0 is returned otherwise.

Since the operation is performed after the increment, ((count++)) , the first time 0 saved, so 1 returned

Note that the same does not happen for pre-increment ((++count)) , since the value is set to 1 , at the very first iteration.

 $ unset count $ count=0 $ echo $? 0 $ ++count -bash: ++count: command not found $ echo $? 127 $ ((++count)) $ echo $? 0 
+10
source

Source: https://habr.com/ru/post/1013555/


All Articles