Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of using for Loop in bash shell script

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the method of using for loop in bash shell script, which is very detailed and has certain reference value. Friends who are interested must finish it!

The = loop is useful for any programming language to execute repetitive statements. The for loop can also be used for bash scripts. In this article, we will introduce the usage and examples of for loops.

=

Syntax:

For VARIABLE in PARAM1 PARAM2 PARAM3do// commands to executedone

In the above syntax, PARAM1,PARAM2 and PARAM3 are passed as parameters. These parameters can be numbers, strings, or file names. The For loop will be executed three times according to the number of parameters passed in the above syntax. VARIABLE is a variable initialized one by one with parameter values.

Example of a for loop in a bash script

To define the number of loops, we simply pass the number as a parameter to the variable.

Foriin1 2 3 4 5 6do echo "$I" done

We can also define ranges instead of writing each number on the latest version of bash. To define the scope, we use curly braces, such as {STARTNUMBER..ENDNUMBER}.

Foriin {1.. 6} do echo "$I" done

We can also pass a string value as a parameter that defines the number of iterations.

For i in SUN MON TUE WED THU FRI SATdo echo "This is $I" done

We can also pass all file names as parameters to the loop.

Foriin*do echo "This file is $I" done

Create a c-like for loop in a bash script

We can also create C-like code for loops in shell scripts.

Syntax:

Or ((EXPR1; EXPR2; EXPR3)) do// commands to executedone

When EXPR1 is used for initialization, EXPR2 is used for conditions, and EXPR3 is used for increments / decrements of variable values.

For example, to execute a loop 10 times, we can simply write a for loop

For ((iTun1; I)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report