CertGrid CertGrid
Hands-on Lab·Red Hat Certified System Administrator

Shell Loops with for, while and until

One objective, phrased as "process file, command line input" - so the forms that matter are the ones that read a file and the ones that read a command's output. Seven loop forms are run here, and one common mistake is shown failing.

Shell Scripts Guide 63 of 67 Beginner

while IFS= read -r LINE is the correct way to read a file line by line. IFS= stops leading and trailing whitespace being stripped and -r stops backslashes being interpreted. Without both, a line containing a backslash or indentation comes out altered.

Every command on this page runs on RHCSA-A01.
Server NameIP AddressOSRolesCPURAMHDD
RHCSA-A01192.168.0.31RHEL 10.0Practice node (graded) - spare /dev/sda2 Core4 GB50 GB + 15 GB

This guide includes

Use this for one objective phrased as processing files and command-line input. This matters because the loop form follows from the input - and brace expansion happens before variables exist, which is why one common attempt quietly produces nothing useful.

Before you start

  1. Seven forms, run

  2. Why `{1..$TOP}` does not work

Official sources