# for i in {1..5} do echo "New File $i" > file$i done
-bash: syntax error near unexpected token `>'
I tried the above single line script which failed with the error above. I'm trying to automate the process of typing:
echo "New File" > file1
echo "New File" > file2
echo "New File" > file3
echo "New File" > file4
echo "New File" > file5
I'm trying to do this without creating a script file such as below which works:
#!/usr/bin/env bash
for i in {1..5}
do
echo "New File $i" > file$i
done
This script works but I want to be able to do something like this on a single line from the command line.
I understand the problem has something to do with the redirection >
to the file. I tried escaping the >
redirection a few other things, and googling but I didn't come up with something which worked.
The reason I want to be able to do this in a single line on the cli is because I want to be able to keep changing the numbers and contents of files for testing and I'd prefer to just do it on the command line rather than editing a script file.
I tried the command separators as suggested and got the error below:
# for i in {1..5} do ; echo "New File $i" > file$i ; done
-bash: syntax error near unexpected token `echo'
I did the command separators incorrectly above in Update 1. Done with the command separators as they are in the answer below worked like a charm.
You need either a semi-colon or a new line before the do
and before the done
. You do not need one after the do
, although it doesn't hurt.
for i in {1..5}; do echo "New File $i" > file$i; done
^ ^
for i in {1..5}
do
echo "New File $i" > file$i
done
for i in {1..5}; do
echo "New File $i" > file$i
done
The following works for me, all you needed to do to your update 1 was to insert a command separator between the for
and the do
:
for i in {1..5} ; do echo "New File $i" >file$i ; done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With