Skip to content

Instantly share code, notes, and snippets.

@tcpdump-examples
Created March 23, 2021 14:05
Show Gist options
  • Save tcpdump-examples/8145bf2e8a3ed2921ddfebd085adaae5 to your computer and use it in GitHub Desktop.
Save tcpdump-examples/8145bf2e8a3ed2921ddfebd085adaae5 to your computer and use it in GitHub Desktop.

This type of for loop is characterized by counting. The range is specified by a beginning (#1) and ending number (#5). The for loop executes a sequence of commands for each member in a list of items. A representative example in BASH is as follows to display welcome message 5 times with for loop:


#!/bin/bash
for i in 1 2 3 4 5
do
   echo "Welcome $i times"
done

Sometimes you may need to set a step value (allowing one to count by two’s or to count backwards for instance). Latest bash version 3.0+ has inbuilt support for setting up ranges:

#!/bin/bash
for i in {1..5}
do
   echo "Welcome $i times"
done

This is from Bash For Loop Examples In Linux

Bash v4.0+ has inbuilt support for setting up a step value using {START..END..INCREMENT} syntax:

#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..2}
  do 
     echo "Welcome $i times"
 done

Sample outputs:

Bash version 4.0.33(0)-release...
Welcome 0 times
Welcome 2 times
Welcome 4 times
Welcome 6 times
Welcome 8 times
Welcome 10 times

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment