How Do You Use the seq Reserved Word in a Bash Script to Generate Content for a File?

Problem scenario
You want to rapidly create hundreds of lines of text for a file. You want to do some testing and you need a file you can delete. You also want to use the seq keyword.

Solution
Here is a program that uses the seq keyword:

#!/bin/bash
COUNTER=100

for varnum in `seq $COUNTER`
do
  date >> goodfile
  echo $varnum >> goodfile
done

Some people recommend not using the seq keyword. An alternative way of writing the above is this:

#!/bin/bash

for varnum in {1..100}
do
  date >> goodfile
  echo $varnum >> goodfile
done

You notice the {1..100} is more terse with no COUNTER variable assignment.

Leave a comment

Your email address will not be published. Required fields are marked *