How to change values of bash array elements without loop

前端 未结 2 1532
谎友^
谎友^ 2020-12-01 04:11
array=(a b c d)

I would like to add a character before each element of the array in order to have this

array=(^a ^b ^c ^d)
<         


        
2条回答
  •  感动是毒
    2020-12-01 04:58

    This way also honor whitespaces in array values:

    array=( "${array[@]/#/^}" )
    

    Note, this will FAIL if array was empty and you set previously

    set -u
    

    I don't know how to eliminate this issue using short code...

提交回复
热议问题