Shell – Execute string result from shell script as a set of shell commands

commandshellshell-script

I create a string result from my shell script and i want to execute it as a shell command.

For example file_a is:

user1@gmail.com
user2@gmail.com

script:

awk '{print "mail -s \"welcome\"", $1}' file_a

And result:

mail -s "welcome" user1@gmail.com
mail -s "welcome" user2@gmail.com

So i want to execute the result as a set of shell commands.

Best Answer

You want to look into the eval command.

eval $(awk '{print "mail -s \"welcome\"", $1}' file_a)

EDIT: You're right @manatwork--eval as I demonstrated wouldn't really work for multiple lines. I was trying to answer Navid's question as he asked it, but really I shouldn't have been afraid to ask him why he wasn't just using a for loop, i.e.:

for m in $(<file_a); do mail -s welcome $m
Related Question