HowTo: Use bash For Loop In One Line

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

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.