0

files=("Benjamin Johnson" "Bastin Johnson" "Bagio Johnson")

( IFS=','; echo "${files[*]/#/Mr.}"; echo "${files[*]/ /_}" )

Expected Result Mr.Benjamin_Johnson,Mr.Bastin_Johnson,Mr.Bagio_Johnson

Output result:

Mr.Benjamin Johnson,Mr.Bagio Johnson,Mr.Bastin Johnson

Benjamin_Johnson,Bagio_Johnson,Bastin_Johnson

2 Answers2

1

Or use printf

$ printf "Mr.%s,Mr.%s,Mr.%s" ${files[@]/ /_}
Mr.Benjamin_Johnson,Mr.Bastin_Johnson,Mr.Bagio_Johnson

Or just one %s

$ printf "Mr.%s," ${files[@]/ /_}
Mr.Benjamin_Johnson,Mr.Bastin_Johnson,Mr.Bagio_Johnson,

But that will add last comma, which can be removed like this

$ printf -v test "Mr.%s," ${files[@]/ /_}; echo ${test%,}
Mr.Benjamin_Johnson,Mr.Bastin_Johnson,Mr.Bagio_Johnson
Ivan
  • 6,188
  • 1
  • 16
  • 23
0

Just use an intermediate array.

( IFS=','; files=("${files[@]/#/Mr.}"); echo "${files[*]/ /_}" )
KamilCuk
  • 120,984
  • 8
  • 59
  • 111