From the course: Cert Prep LPIC-1 Exam 102 (Version 5.0)

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Iterate with for loops

Iterate with for loops - Linux Tutorial

From the course: Cert Prep LPIC-1 Exam 102 (Version 5.0)

Start my 1-month free trial

Iterate with for loops

- [Narrator] Whenever you want to loop through a finite list of items, you may want to use a for loop. The syntax for the for loop looks like this. We start with a list of items to loop through, as we iterate through them, they're assigned to a variable. In this example, the variables name is item. Inside the loop, we do something with the contents of the variable. The list for the for loop can come from any number of sources. It can be a static list that we create such as one, two, three, four, five, or a list of names. If your looking for a list of sequential numbers, we can create a dynamically. In the past, we'd use a sequence command for this. And we'd use command substitution to get it to run. However, it's better to have Bash create the list using expansion. This way we're not spawning a new shell and executing commands. It's more reliable and it's faster. There are times though when we will use command…

Contents