A concise tutorial on shell script for loop statements _linux Shell

Source: Internet
Author: User

Like other programming languages, the shell supports A for loop.

For loops The general format is:

Copy Code code as follows:

For variable name in list
Todo
Command1
Command2
...
CommandN
Done

When the value of the variable is in the list, the For loop executes all the commands at once, using the variable name to get the current value in the list. The command can be any valid shell command and statement. The in list can contain replacements, strings, and file names.

The in list is optional, and if it is not used, the For loop uses the positional parameters of the command line.

For example, sequentially prints a number in the current list:

Copy Code code as follows:

For loop in 1 2 3 4 5
Todo
echo "The value is: $loop"
Done

Output:
The value is:1
The value Is:2
The value Is:3
The value Is:4
The value Is:5

Characters in the sequential output string:

Copy Code code as follows:

For str in ' it ' a string '
Todo
Echo $str
Done

Output:
This is a string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.