bash 4.2
the equation
i=$(( (i+1) %3 ))
I want left i to be 0 when the right i is wrong input like null.
something like i=${((i+3) %3):=0} if I can.
Let's try with:
i=$(( ( ${i-0} + 1) %3 ))
1 on my system1%3 is 1.${i-0} handles only cases where i is not set. For example i=,; echo $(( ${i-0} )) results in bash: , : syntax error and i=k; k=7; echo $(( ${i-0} )) prints 7 (note that we used i=k, not i=$k. Inside an arithmetic context variables can be full-blown arithmetic expressions, not just numbers)
right ican be empty or have trash valuesifirst, and then do the calculation and the assignment.ias an integer beforehand (declare -i i) so that it can't be assigned trash, or validate its value using acaseor[[command first.