Search code examples
bashlinux-kernelredhatcommand-substitutionparameter-expansion

How to apply multiple parameter expansion in BASH for a single output?


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


Solution

  • Just use an intermediate array.

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