Linux Shell Series Tutorial (10) Shell for Loop

Source: Internet
Author: User

This article is part of the Linux Shell Series Tutorial (10), more Linux shell tutorials: Linux Shell Tutorials

Basically any language has its own looping statements, of course, the shell is no exception, today we introduce the use of shell for loop.

Shell for Loop syntax

The syntax for the Shell for loop is as follows

For variable in list do    Command1    command2 ...    Commandndone

A list is a sequence of values (numbers, strings, and so on), each separated by a space. Once per loop, the values in the list are placed sequentially into the specified variable, and then the command area is repeated (between do and done) until all elements have been exhausted.

Shell for Loop Example

The following example shows you how to use the shell for loop.

For loop in one II tree Fourdo    echo "I am: $loop" done

Output Result:

I am:onei Am:twoi am:treei am:four

You can sequentially output a string of words, example:

For STR in I am Linuxdaxuedo    Echo $strdone

Output Result:

Iamlinuxdaxue

You can display the files in the current directory, for example:

For file in./*do    Echo $filedone

Output Result:

./a.sh./automake./automonitor./crzk./makedir./shell_start./sql./submit

The shell for loop command is easier to master, so let's introduce it to you today. For more shell tutorials See: Linux Shell Series Tutorials

This article by Linux technology talent "Daxue" published in: Linux University
This article fixed link: Linux Shell series Tutorial (10) Shell for Loop

Linux Shell Series Tutorial (10) Shell for Loop

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.