Skip to content

Instantly share code, notes, and snippets.

@samy-mahmoudi
samy-mahmoudi / iterate_shell_array.sh
Last active June 15, 2020 15:41 — forked from biiont/iterate_shell_array.sh
Iterate over an array using POSIX Shell
#!/bin/sh
# Iterate over an array using POSIX Shell
# Initial data
ARR="a:b:c:d"
# Iteration. Do whatever is needed instead of 'echo "$CAR"'.
CDR="${ARR}:"; while [ -n "$CDR" ]; do CAR=${CDR%%:*}; echo "$CAR"; CDR=${CDR#*:}; done; unset CAR CDR
# IMPORTANT!!! Add semicolon to the end of an array (IT="${ARR}:") to make stop condition working.