0

I am writing a bash script which runs through the numbers 1 - 50 and I have to output every number except numbers which are multiples of 4 (ex. 4, 8, 12..). I have tried using an example from a similar question which asks to output the same array except certain numbers.

In the code attached my program will output every number except 3.

    #!/bin/bash
LIMIT=49
echo "Printing multiples of 4 from 1 - 50: "
a=0
while [ $a -le $LIMIT ];do
    a=$(($a+1))
    if [$a -eq 3]
    then 
        continue
    fi
    echo -n "$a"
done

How should I change the IF statement to output my desired script?

1
  • 1
    Why not just seq 49 | sed '4~4d'? Commented Apr 2, 2020 at 13:11

1 Answer 1

2
for num in {0..50}; do
  if (( num % 4 )); then 
    echo $num
  fi
done

This is not as brief as sed version, but it shows how to achieve it with bash built-ins only.

Sign up to request clarification or add additional context in comments.

2 Comments

Although this is more or less on the right track, how would I change the code if I wanted the output to 'not' be multiples of 4? Ex: 1,2,3,5,6,7,9
@MiSully huh? This is exactly what it outputs, non multiples of 4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.