-
-
Save biiont/290341b29657c0bb2df6 to your computer and use it in GitHub Desktop.
Iterate over an array using POSIX Shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. | |
# Otherwise you will get infinite loop. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, nice work!
Forked and revised: you forgot to change $VAL to $CAR in the comment of line 7, at revision 5.