The for loop is the iteration statement that executes a set of commands repeatedly until a terminating condition. It works on lists of items and repeats commands for every defined item. Using the for loop in a command line or the shell scripts is quite straightforward, and the user can manipulate its structure as per requirements.
Keeping this in view, this guide shows the complete procedure to make a for loop in a command line.
- Make a for Loop in Command Line
- Print Set Of Numbers
- Display a Number Series
- Print the Values in a Non-Sequential Manner
- Print Character Strings
How to Make a for Loop in Command Line?
The working of the for loop relies on its generalized syntax that is typed below:
General Syntax:
for item in [list]
do
[Commands]
done
The for loop can be executed in a one-line code, whose syntax is provided below:
Syntax of One Line For Loop:
for item in [list]; do [Commands]; done
The above syntax holds the following parameters:
- The for loop works on lists of items. The list contains numeric and character values. It repeats the set of commands for every item specified in the list.
- The do and done are the starting and ending keywords of for loop
Both the syntaxes of the for loop offers the same functionality. So, the users can either utilize the one-line or the general syntax of for loop.
Example 1: Print Set Of Numbers
The interactive nature of the “for” Loop allows the user to print the numbers simultaneously instead of using multiple print statements. Let’s see how it works:
$ for num in 1 2 3 4 5 6 7 8
> do
> echo "The number is "$num
> done
- Eight “8” numbers are defined in the list that will be stored in the “num” variable.
- The loop will iterate 8 times, each time setting a new “num” variable value.
- The “echo” command will print the “num” value inside the loop:
The output shows that multiple lines are printed in the terminal. Each time the “num” variable is printed, a new value is defined in the list
Example 2: Display a Number Series
Instead of typing each number one by one in the list, use the brace expansion, i.e., {#…#} to create a sequence. Let’s understand it through the following example:
$ for num in {1..20}; do echo "The new number is "$num; done
Here the numeric series “{1..20}” is declared in “for” loop that will execute the “do” part 20 times. This example uses the one-line “for” loop.
The output is the same as the “Example 1”, and all the “$num” values are printed sequentially.
Example 3: Print the Values in a Non-Sequential Manner
To iterate the for loop nonsequentially, the user can set the step values using the “brace expansion”.
for i in {1..30..5}; do echo "The value is=$i"; done
In this example, the for loop contains the series {1..30} that will print out the “i” variable value after the specified gap of “5” numbers.
The output shows the “$i” value by following the gap of 5 numbers.
Example 4: Print Character Strings
For loop is not only restricted to integers. It can be iterated through string values and characters.
The following example shows the practical implementation of string using the generalized syntax of “for” loop:
$ for name in Anna Johnson Herry
> do
> echo "The name is "$name
> done
The for loop defines the three strings values in the “lists” and will print using the “echo” command:
Note: To use the “for” loop in a bash script read our detailed article using the provided link “ Master the Bash For Loop: A Beginner’s Guide”
Conclusion
In Linux, the “for” loop executes the block of statements repeatedly for every item in a list. It offers two types of syntax, i.e., “generalized” and “one line”. The for loop can simplify the multiple lines of code into fewer ones.
This post provided a detailed view to make a for loop in the command line.