Skip to content

Instantly share code, notes, and snippets.

@dexterous
Created October 7, 2017 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dexterous/15be43f02fa2a134c120aec187509ab3 to your computer and use it in GitHub Desktop.
Save dexterous/15be43f02fa2a134c120aec187509ab3 to your computer and use it in GitHub Desktop.
A succinct bash fizzbuzz
#!/bin/bash -e
#set -o xtrace
if [[ $# -lt 1 ]]; then
echo '
Usage: fizzbuzz upto
upto: The number upto which to count
'
exit 1
fi
for x in $(seq 1 "${1}"); do
RESULT=''
if [[ $((x % 3)) -eq 0 ]]; then
RESULT="${RESULT}Fizz"
fi
if [[ $((x % 5)) -eq 0 ]]; then
RESULT="${RESULT}Buzz"
fi
echo "${RESULT:-${x}}"
done
@dexterous
Copy link
Author

Execute like so: ./fizzbuzz 100 | cat -n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment