From the course: CompTIA Linux+ (XK0-005) Cert Prep

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

For loop

For loop

- [Instructor] 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 the list of items to loop through. As we iterate through them, they're assigned to a variable. In this example, the variable's 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 1, 2, 3, 4, 5, or a list of names. If we're looking for a list of sequential numbers, we can create it 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 is more reliable and it's faster. There are times though when we will use command substitution to…

Contents