Skip to content

Instantly share code, notes, and snippets.

@biiont
biiont / iterate_shell_array.sh
Last active February 7, 2023 18:00 — forked from anonymous/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 "$VAL"'.
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.