DomainsFeatured DomainsFeatured - 2 months ago 7
Linux Question

Append Variable To Every Line Of Another Variable

I would like to take the following data which is stored in

$variable1
:

term 1
term 2
term 3


And append
$variable2
which contains:

addition


So, it becomes:

term 1,addition
term 2,addition
term 3,addition


The trick is that it has be done using both as variables. I was thinking echo, paste, awk or sed. It could be something like this:

while read line $variable1; do echo "$line,$variable2"; done


I have played around with them but when variables and quoting get involved, I mess it up. Any help is greatly appreciated.

Answer

With GNU bash and a here string:

while IFS='' read -r line; do echo "$line,$variable2"; done <<< "$variable1"

Output:

term 1,addition
term 2,addition
term 3,addition
Comments