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

Written against the versions above. `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.0 (Coughlan)Practice node (graded) - spare /dev/sda2 Core4 GB50 GB + 15 GB

Before you start

  1. Seven forms, run

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

Official sources