Linux bash for loop

Q. How do I use bash for loop under Linux operating systems?

A. for loop is used to process group of files or for other automation purposes.

Linux bash for loop syntax

for variable in list
do
   echo $variable
done

To loop through all files in a directory, enter:

for file in *
do
   echo $file
done

To print number 1 to 10, enter:

for n in {1..10}
do
   echo $n
done

Shell script to print odd and even number using a for loop

#!/bin/bash
for n in {1..10}
do
   out=$(( $n % 2 ))
   if [ $out -eq 0 ] 
   then
	echo "$n is even number"
   else
	echo "$n is ODD number"	
   fi	
done

See more bash for loop examples here.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Mosco Feb 10, 2012 @ 5:09

    My Epson CX3810 works out of the box with no fuss, that is the ptnerir and the scanner part.. Faxing should also work, tho I have not tried it. I recently moved the ptnerir to an Ubuntu home and shared it out and I can to it on my Linux boxes and Windows and even my old Mac PPC G4 tower running OS 10.5.. Of course the Windows (XP/Vista and 7) all needed drivers and so did the Mac..Just in case anyone wanted to know.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum