Bash FizzBuzz


And another one, this time in Bash.

#!/bin/bash

for I in `seq 1 100`; do
        if [ "`expr $I % 15`" == "0" ]; then
                echo -n "FizzBuzz "
        elif [ "`expr $I % 3`" == "0" ]; then
                echo -n "Fizz "
        elif [ "`expr $I % 5`" == "0" ]; then
                echo -n "Buzz "
        else
                echo -n "$I "
        fi
done

2 Responses to “Bash FizzBuzz”

  1. peter Says:

    Oh nice! My initial crack at bash was:

    for i in {1..100}; do if [ $(($i%3)) -eq 0 ] ; then echo fizz ; elif [ $(($i%5)) -eq 0 ] ; then echo buzz ; fi ; if [ $(($i%3)) -eq 0 -a $(($i%5)) -eq 0 ] ; then echo fizzbuzz ; else echo $i ; fi ; done

    I think yours is better.

  2. coelmay Says:

    for i in `seq 100`;do if(($i%15==0));then echo FizzBuzz;elif(($i%5==0));then echo Buzz;elif(($i%3==0));then echo Fizz;else echo $i;fi;done;

Leave a Reply