HowTo: Use bash For Loop In One Line

by on January 7, 2010 · 1 comment· last updated at 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
 

OR

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

Work On Files

 
for i in *; do echo $i; done
 

OR

 
for i in /etc/*.conf; do cp $i /backup; done
 


You should follow me on twitter here or grab rss feed to keep track of new changes.

Featured Articles:

{ 1 comment… read it 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
???

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <kbd> <blockquote> <pre> <a href="" title="">

Tagged as: , , , , , , ,

Previous Faq:

Next Faq: