Home > Enterprise >  Print multiple of 3 and 5 in bash script
Print multiple of 3 and 5 in bash script

Time:10-07

Write a short program that prints each number from 1 to 100 on a new line. • For each multiple of 3, print "Fizz" instead of the number. • For each multiple of 5, print "Buzz" instead of the number. • For numbers which are multiples of both 3 and 5, print "FizzBuzz" instead of the number.

This what i have so far:

#!/bin/bash


*for i in {0..20..5}
do
  echo "Number: $i"
done*

but i get this:

Number: {0..20..5}

Need help

CodePudding user response:

If it's on a mac, use $(seq 0 5 20) to get the same result.

CodePudding user response:

Using GNU sed

$ for ((i=1;i<=100;i  )); do echo "$i"; done | sed -E '0~3s/[0-9] /Fizz/;0~5s/[0-9] |$/Buzz/'
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
  • Related