Prefix and postfix elements of a bash array
Bash brace expansion don't use regexes. The pattern used is just some shell glob, which you can find in bash manual 3.5.8.1 Pattern Matching.
Your two-step solution is cool, but it needs some quotes for whitespace safety:
ARR_PRE=("${ARRAY[@]/#/prefix_}")echo "${ARR_PRE[@]/%/_suffix}"
You can also do it in some evil way:
eval "something $(printf 'pre_%q_suf ' "${ARRAY[@]}")"
Prettier but essentially the same as the loop solution:
$ ARRAY=(A B C)$ mapfile -t -d $'\0' EXPANDED < <(printf "prefix_%s_postfix\0" "${ARRAY[@]}")$ echo "${EXPANDED[@]}"prefix_A_postfix prefix_B_postfix prefix_C_postfix
mapfile
reads rows into elements of an array. With -d $'\0'
it instead reads null-delimited strings and -t
omits the delimiter from the result. See help mapfile
.