Skip to content

Instantly share code, notes, and snippets.

Created August 20, 2015 09:50
Show Gist options
  • Save anonymous/c59b9cf535e681e41a0c to your computer and use it in GitHub Desktop.
Save anonymous/c59b9cf535e681e41a0c to your computer and use it in GitHub Desktop.
# Iterate over some array using POSIX Shell
# Initial data
ARR="a:b:c:d"
# Iteration. Do whatever is needed instead of 'echo "$VAL"'.
IT="${ARR}:"; while [ -n "$IT" ]; do VAL="${IT%%:*}"; echo "$VAL"; IT="${IT#*:}"; done
# IMPORTANT!!! Add semicolon to the end of an array (IT="${ARR}:") to make stop condition working.
# Otherwise you will get infinite loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment