Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»Working with FOR Loop in Bash Shell with Examples

    Working with FOR Loop in Bash Shell with Examples

    RahulBy RahulJanuary 7, 20152 Mins Read

    Loops are very useful for doing repetitive tasks for any programming language. For loops are also available for bash scripting. In this article we will learn about uses of for loops with useful examples.

    Syntax:

    for VARIABLE in PARAM1 PARAM2 PARAM3
    do
    // commands to execute
    done

    In above syntax PARAM1, PARAM2 and PARAM3 are parameters passed as an arguments. These parameters can be a number string or file names. For loop will be executed 3 times as per numbers of parameters passed in above syntax. VARIABLE is a variable which is initialized one by one using parameter values.

  • Read: While Loop Examples in Bash Scripting
  • Examples of For Loop in Bash Script

    To define number of number of iteration’s for a loop, we simply pass numbers as an argument for variable.

    for i in 1 2 3 4 5 6
    do
       echo "$i"
    done
    

    We can also define range in place of writing each number on latest version of bash. To define a range we use curly braces like {STARTNUMBER..ENDNUMBER}.

    for i in {1..6}
    do
       echo "$i"
    done
    

    We can also pass string values as parameters for defining number of iterations and pass as argument

    for i in SUN MON TUE WED THU FRI SAT
    do
       echo "This is $i"
    done
    

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

    for i in *
    do
       echo "This file is $i"
    done
    

    Creating C Like For Loop in Bash Script

    We can also create C like for loops inside a shell script

    Syntax:

    for ((EXPR1; EXPR2; EXPR3))
    do
    // commands to execute
    done

    Where EXPR1 is used for initialization, EXPR2 is used for condition and EXPR3 is used for increment/decrement of variable values.

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

    for ((i=1; i<=10; i++))
    do
      echo "$i"
    done
    
    bash for loop script
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Install Elasticsearch on CentOS 7/6
    Next Article Working with WHILE Loop in Bash Shell Scripting

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Bash Select Command (Create Menu in Shell Scripts)

    Updated:December 10, 20212 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    Bash String Concatenate

    Updated:January 5, 20222 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.