From 8bd977f5d3f0a88c96f97d5b2da16f94890433ca Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Fri, 15 Jun 2018 16:35:22 +1000 Subject: [PATCH] Added loops --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index f62e730..beaeee5 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,9 @@ scripts and not full blown utilities. * [Toggle between two values.](#toggle-between-two-values) * [Loops](#loops) * [Loop over a range of numbers.](#loop-over-a-range-of-numbers) + * [Loop over a variable range of numbers.](#loop-over-a-variable-range-of-numbers) * [Loop over an array.](#loop-over-an-array) + * [Loop over an array with an index.](#loop-over-an-array-with-an-index) * [Loop over files and directories.](#loop-over-files-and-directories) * [File handling](#file-handling) * [Read a file to a string.](#read-a-file-to-a-string) @@ -565,7 +567,13 @@ Don't use `seq`. for i in {0..100}; do printf '%s\n' "$i" done +``` +## Loop over a variable range of numbers. + +Don't use `seq`. + +```shell # Loop from 0-VAR. VAR=50 for ((i=0;i<=VAR;i++)); do @@ -582,7 +590,11 @@ arr=(apples oranges tomatoes) for element in "${arr[@]}"; do printf '%s\n' "$element" done +``` +## Loop over an array with an index. + +```shell # Elements and index. for i in "${!arr[@]}"; do printf '%s\n' "${arr[$i]}"