$ {arr [@]} on sed

#!/bin/sh

arr=(
    a
    b
    c
)
sed "s/abc/${arr[@]}/" file

sh -x this_script.sh shows the result with an error:

+ arr=(a b c)
+ sed s/abc/a b c/ file
sed: -e expression #1, char 5: unterminated `s' command

it should be:

+ sed 's/abc/a b c/' file

this script already has a double quote, why you need to declare a variable for it to work:

 x=${arr[@]}
 sed "s/abc/$x/" file
+4
source share
1 answer

You can use ${arr[*]}instead ${arr[@]}to treat it as a separate line:

sed "s/abc/${arr[*]}/" <<< "abc"

a b c
+2
source

Source: https://habr.com/ru/post/1653987/


All Articles