Shell Script Learning 18 shell for loop

Source: Internet
Author: User

Similar to other programming languages, the shell supports A for loop.

The general format for A for loop is:

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. Each time the loop is passed, the next value in the list is assigned to the variable.

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

For example, sequentially output the numbers in the current list:

    1. For Loop in 1 2 3 4 5
    2. Do
    3. Echo "The value is: $loop"
    4. Done

Operation Result:

The value is:1the value is:2the value is:3the value is:4the value is:5


Characters in the sequential output string:

    1. For str ' A string '
    2. Do
    3. echo $str
    4. Done

Operation Result:

This is a string


Show files starting with. Bash in the home directory:

    1. #!/bin/bash
    2. For FILE in $HOME/.bash*
    3. Do
    4. echo $FILE
    5. Done

Operation Result:

/root/.bash_history/root/.bash_logout/root/.bash_profile/root/.bashrc

Shell Script Learning 18 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.