Prefix and postfix elements of a bash array

前端 未结 5 1761
-上瘾入骨i
-上瘾入骨i 2020-12-14 16:13

I want to pre- and postfix an array in bash similar to brace expansion.

Say I have a bash array

ARRAY=( one two three )

I want to b

5条回答
  •  执念已碎
    2020-12-14 16:30

    I have exactly the same question, and I come up with the following solution using sed's word boundary match mechanism:

    myarray=( one two three )
    newarray=( $(echo ${myarray[*]}|sed "s/\(\b[^ ]\+\)/pre-\1-post/g") )
    echo ${newarray[@]}
    > pre-one-post pre-two-post pre-three-post
    echo ${#newarray[@]}
    > 3
    

    Waiting for more elegant solutions...

提交回复
热议问题