Table of Contents

About

The loop structures in Bash are:

You can use the break and continue statement to control the loop behavior.

Example

Atomic String

When calling a loop, quote the variable otherwise bash will not see the string as atomic.

output_args()
{
  for arg
  do
    echo "$arg"
  done
}
var="hello Nico"

echo "Without Quotes"
echo "**************"
output_args $var
echo 
echo "With Quotes"
echo "**************"
output_args "$var"
Without Quotes
**************
hello
Nico

With Quotes
**************
hello Nico

Parsing of command output

ls -l | while IFS=" " read -r permission child owner group size monthDate dayDate rest; do
    echo Line: 
    echo '    - Permission: '$permission
    echo '    - Child: '$child
    echo '    - Owner: '$owner
    echo '    - Group: '$group
    echo '    - Size: '$size
    echo '    - Month of Date: '$monthDate
    echo '    - Day of Date: '$dayDate
    echo '    - Rest: '$rest
done
Line:
    - Permission: -rw-r--r--
    - Child: 1
    - Owner: root
    - Group: root
    - Size: 1849173
    - Month of Date: May
    - Day of Date: 13
    - Rest: 2015 unixODBC-2.3.2.tar.gz
Line:
    - Permission: -rwxr-xr--
    - Child: 1
    - Owner: oracle
    - Group: oinstall
    - Size: 457
    - Month of Date: May
    - Day of Date: 16
    - Rest: 2017 whileDemo.sh