Monthly Archives: December 2014

Fizzbuzz in bash

I’ve been able to get by in bash for a long time. I could read it just fine, but if I wanted to write it, I needed a cheat sheet by my side. I’ve decided that that wasn’t good enough–I should be actively practicing it, so that I know it off the top of my head.

Here’s a fizzbuzz implementation.


#!/usr/bin/env bash

topval=${1:-100}

for i in $(seq 1 $topval);
do
	if [ "$(($i % 15))" -eq "0" ]
	then
		echo fizzbuzz!
	elif [ "$(($i % 3))" -eq "0" ]
	then
		echo fizz!
	elif [ "$(($i % 5))" -eq "0" ]
	then
		echo buzz!
	else
		printf '%d\n' $i
	fi
done