Add 3 , and then Mod 3 to the first result set:
$ for i in {-5..5}; do printf "%d " $(( (($i % 3) + 3) % 3 )) ; done 1 2 0 1 2 0 1 2 0 1 2
If you know the maximum range, you can simply add a significantly larger number of multiples of 3 to make all numbers positive until the first modulo operation.
$ for i in {-5..5}; do printf "%d " $(( ($i + 3000000) % 3 )) ; done
However, the first approach is cleaner and more universal.
Finally, for fun:
positive_mod() { local dividend=$1 local divisor=$2 printf "%d" $(( (($dividend % $divisor) + $divisor) % $divisor )) } for i in {-5..5}; do printf "%d " $(positive_mod $i 3) done
source share