Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Bash for loop

farenda 2015-06-03 0

Problem:

In Bash you can use for loop to iterate over a number of things. This tutorial shows how to use two Bash for loops in a simple way!

Solution:

Bash has two for loops:

  1. C-style loop that iterates through an index variable:
    # semicolon is required when "do" is on the same line
    for (( expr1; cond; expr2 )); do
       commands...
    done
    

    Example:

    $> for ((i=0; i < 3; ++i)); do echo $i; done
    0
    1
    2
    
  2. Another kind of for loop is for-each kind:
    for VAR in SEQUENCE; do
       commands...
    done
    

    sequence can be any list of things, like files, output of commands, etc. Examples will clarify

    Use for loop to iterate over list of files:

    $> for f in /bin/c*; do ls $f; done
    /bin/cat
    /bin/chacl
    /bin/chgrp
    /bin/chmod
    /bin/chown
    /bin/chvt
    /bin/cp
    /bin/cpio
    

    for loop to iterate over list of numbers:

    $> for i in 1 2 3; do echo $i; done
    1
    2
    3
    

    for loop to iterate over generated range of numbers:

    # from 0 to 6, with step 2
    $> for i in {0..6..2}; do echo $i; done
    0
    2
    4
    6
    

    for loop to iterate over result lines of subcommand:

    $> for i in $(ls /bin/d*); do echo $i; done
    /bin/dash
    /bin/date
    /bin/dd
    /bin/df
    /bin/dir
    /bin/dmesg
    /bin/dnsdomainname
    /bin/domainname
    /bin/dumpkeys
    

    When [in LIST] is not specified, then $@ (input parameters) is used.

    Bash for loops are really powerful and flexible. Use them in your shell scripts and command line!

Share with the World!
Categories Bash Tags bash
Previous: Gradle Java version
Next: Spock Framework expect with where block

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored