Table of Contents
Loop in Unix // Linux: For Loop in Shell is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
The for loop works on lists of items (items). It repeats a set of commands for each item in a list.
Syntax in Unix / Linux
for var in word1 word2 . wordN do cac lenh de thuc thi cho moi word . done
Here, var is the name of a variable and word1 to wordN is a sequence of characters separated by spaces. Each time the loop executes, the value of the var variable is set to the next word in the list of words, from word1 to wordN.
As an example, in Unix / Linux
Here is a simple example that uses a for loop to browse the list of numbers.
#!/bin/sh for var in 0 1 2 3 4 5 6 7 8 9 do echo $var done
It will produce the following result:
0 1 2 3 4 5 6 7 8 9
Here is an example to display all files starting with .bash and available in your home directory. I made this script from my root .
#!/bin/sh for FILE in $HOME /. bash * do echo $FILE done
FAQ
What should you know about syntax in Unix / Linux for var in word1 word2 . wordN do cac lenh de thuc thi cho moi word . done Here, var is the name of a variable and word1 to wordN is a sequence of characters separated by spaces. Each time the loop executes, the value of the var variable is set to the next word in the list of words, from word1 to wordN. As an example, in Unix / Linux?
Here is a simple example that uses a for loop to browse the list of numbers.
What is Loop in Unix // Linux: For Loop in Shell?
The for loop works on lists of items (items).
Why is Loop in Unix // Linux: For Loop in Shell important?
A clear understanding of Loop in Unix // Linux: For Loop in Shell helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
Reader Comments 0
Sign in with email or Google to join the discussion.