#Pure Bash, 28
Pure Bash, 28
eval echo [${1//:/{:,]:[\}}]
Here, lists are colon-separated, and contained in square brackets. For example in the question, the input list would be 1:2:3:4 and the output is:
[1:2:3:4] [1:2:3]:[4] [1:2]:[3:4] [1:2]:[3]:[4] [1]:[2:3:4] [1]:[2:3]:[4] [1]:[2]:[3:4] [1]:[2]:[3]:[4]
${1//:/REPLACEMENT}replaces the colons in$1with{:,]:[\}- This generates a brace expansion like
[1{:,]:[}2{:,]:[}3{:,]:[}4] - The eval (and careful
\escapes) causes the brace expansion to happen last and give the desired result.
If it is necessary to exactly match the given [[ , , ...]] format, then we can do this instead:
#Pure Bash, 47
Pure Bash, 47
eval printf '%s\\n' ${1//, /{\\,\\ ,]\\,\\ [\}}