0

I want to print odd numbers in alpine:3.13 image. I'm getting the error, line 4: arithmetic syntax error. However, this works fine in linux. How can I fix this? Thanks!

for i in {1..10}
do
    if [ $(( $i % 2 )) != 0 ]
    then
      echo "$i"
    fi
done

When I change the first line with this for i in 1 2 3 4. It works fine but I need to give a range. Any help would be appreciated

cosmos-1905-14
  • 783
  • 2
  • 12
  • 23

2 Answers2

0

Brace notation for ranges is a bash extension, which is apparently not available on Alpine.

Use $(seq 1 10) instead:

for i in $(seq 1 10)
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You can try using a more portable syntax.

i=0
while [ "$i" -lt 10 ]; do
  if [ $(( $i % 2 )) != 0 ]
    then
      echo "$i"
    fi
  i=$((i + 1))
done
Jetchisel
  • 7,493
  • 2
  • 19
  • 18