HowTo: Use bash For Loop In One Line

by on January 7, 2010 · 6 comments· LAST UPDATED June 7, 2011


How do I use bash for loop in one line under UNIX or Linux operating systems?

The syntax is as follows to run for loop from the command prompt.

Run Command 5 Times

for i in {1..5}; do COMMAND-HERE; done


 for((i=1;i<=10;i+=2)); do echo "Welcome $i times"; done

Work On Files

for i in *; do echo $i; done


for i in /etc/*.conf; do cp $i /backup; done
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 6 comments… read them below or add one }

1 Kingpin March 21, 2013 at 12:13 pm

OK, but how do I run two commands in the for loop? for () command1; command; done


2 Anne August 22, 2013 at 5:00 pm

two commands:
for i in (); do echo $i ; ls $i; done

nested for:

for i in {0..3}; do for j in {0..9}; do echo -n \($i, $j\); echo Is today the $i$j \?; done; done


3 roger September 25, 2013 at 5:50 pm

I’d suggest you add an example of this to also that would have helped me…maybe with a ToC at the top as well :)


4 w February 13, 2014 at 10:49 am

Thanks for this.


5 ronator October 14, 2014 at 2:41 pm

For the very rare case yout got file names with a leading dash:

for i in *; do echo $i; done [ will not show files with leading dash ]

for i in “*”; do echo $i; done [ will show files with leading dash (but not one file per line) ]


6 Anne February 13, 2015 at 11:08 pm

Love it! Thanks!


Leave a Comment

Tagged as: , , , , , , ,

Previous Faq:

Next Faq: