James
James

Reputation: 105

How to write a bash script with a for loop that creates multiple text files with different names

I would like to write a simple bash script that will allow me to create 5 text files called File1, File2, File3, File4 and File5 each populated with the text "F1", "F2", "F3", "F4" and "F5" respectively.

I have tried the script

for i in 1,2,3,4,5; do
echo "F"$i > File$i
done

But this creates one file called File1,2,3,4,5 with the text in it F1,2,3,4,5.

How do I correct this code?

Thank you very much

Upvotes: 0

Views: 3208

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

Try the below code to Print F1,F2,F3,F4,F5 into five different files File1,File2,File3,File4,File5 seperately.

for i in {1..5}; do echo "F"$i > File$i; done
  • {1..5} Represents the range from 1 to 5.

  • do echo "F"$i > File$i creates a file with the name as File$i and prints the value F$i to that file.(i represents the current value ranges from 1 to 5).

Upvotes: 4

Related Questions