FizzBuzz in one line of Bash

for i in {1..100};do((i%3))&&x=||x=Fizz;((i%5))||x+=Buzz;echo ${x:-$i};done|column
The (in)famous "FizzBuzz" programming challenge, answered in a single line of Bash code. The "|column" part at the end merely formats the output a bit, so if "column" is not installed on your machine you can simply omit that part. Without "|column", the solution only uses 75 characters. The version below is expanded to multiple lines, with comments added. for i in {1..100} # Use i to loop from "1" to "100", inclusive. do ((i % 3)) && # If i is not divisible by 3... x= || # ...blank out x (yes, "x= " does that). Otherwise,... x=Fizz # ...set x to the string "Fizz". ((i % 5)) || # If i is not divisible by 5, skip (there's no "&&")... x+=Buzz # ...Otherwise, append (not set) the string "Buzz" to x. echo ${x:-$i} # Print x unless it is blanked out. Otherwise, print i. done | column # Wrap output into columns (not part of the test).
Sample Output
1        16       31       46       61       76       91
2        17       32       47       62       77       92
Fizz     Fizz     Fizz     Fizz     Fizz     Fizz     Fizz
4        19       34       49       64       79       94
Buzz     Buzz     Buzz     Buzz     Buzz     Buzz     Buzz
Fizz     Fizz     Fizz     Fizz     Fizz     Fizz     Fizz
7        22       37       52       67       82       97
8        23       38       53       68       83       98
Fizz     Fizz     Fizz     Fizz     Fizz     Fizz     Fizz
Buzz     Buzz     Buzz     Buzz     Buzz     Buzz     Buzz
11       26       41       56       71       86
Fizz     Fizz     Fizz     Fizz     Fizz     Fizz
13       28       43       58       73       88
14       29       44       59       74       89
FizzBuzz FizzBuzz FizzBuzz FizzBuzz FizzBuzz FizzBuzz

By: willdye
2017-08-22 15:44:16

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: