# Diff: FizzBuzz

Differences between current version and previous revision of FizzBuzz.

Other diffs: Previous Major Revision, Previous Author

Newer page: | version 2 | Last edited on March 1, 2012 3:27 pm | by PhilHollenback | |

Older page: | version 1 | Last edited on January 18, 2010 3:55 pm | by PhilHollenback | Revert |

`@@ -20,30 +20,7 @@`

`I'd like to hear if anyone can write this in a more compact and/or elegant fashion in bash 3. However, I'm not really interested in going the obfuscated code route - what's the most compact way you can write this script that is still readable and elegant?`

`-----`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`+`CategoryGeekStuff

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`-`

`+`CategoryBlog

## current version

I read this blog posting on a trivial programming test at two in the morning and I got worried: was I an actual programmer? Thus I decided to implement FizzBuzz in bash 3:

for i in {1..100} do ([ $(($i % 15)) -eq 0 ] && echo FizzBuzz ) || \ ([ $(($i % 3)) -eq 0 ] && echo Fizz ) || \ ([ $(($i % 5)) -eq 0 ] && echo Buzz ) || \ echo $i done

A few thoughts:

- Note the use of the new bash 3 builtin iterator
`{1..100}`

to generate the list instead of using a counter variable, seq, or jot. - Note also the use of bash builtin arithmetic evaluation instead of bc or expr.
- I wrote this in the one line style with conditional operators instead of branching. For some reason I am more comfortable in that style.
- Is there a way to make the return value of a math operation be the mathematical result? I couldn't think of one offhand. That leads to the awkward
`[ x -eq 0 ]`

construct. I suspect that could be replaced with a combination of expr and eval.

I'd like to hear if anyone can write this in a more compact and/or elegant fashion in bash 3. However, I'm not really interested in going the obfuscated code route - what's the most compact way you can write this script that is still readable and elegant?