${var#pattern}
is a parameter expansion that expands to the value of $var
with the shortest possible match for pattern
removed from the front of the string.
Thus, ${VAR#*:}
removes everything up and including to the first :
; ${VAR#*:*:}
removes everything up to and including the second :
.
The trailing *
s on the end of the expansions given in the question don't have any use, and should be avoided: There's no reason whatsoever to use ${var#*:*:*}
instead of ${var#*:*:}
-- since these match the smallest amount of text possible, and *
is allowed to expand to 0 characters, the final *
matches and removes nothing.
If what you really want is an array, you might consider using a real array instead.
# read contents of string VAR into an array of states
IFS=: read -r -a states <<<"$VAR"
echo "${states[0]}" # will echo NJ
echo "${states[1]}" # will echo NY
echo "${#states[@]}" # count states; will emit 3
...which also gives you the ability to write:
printf ' - %s\n' "${states[@]}" # put *all* state names into an argument list