Richard
Richard

Reputation: 15542

how to execute two commands stored in one bash variable

Let's say there is one bash variable

run1="date"

I need to execute date by

${run1}

And it works, since it prints current time. But if I put two commands in the variable,

run2="date; echo foo"

I can't execute the commands stored in variable run2, since ${run2} complains

date;: command not found

Upvotes: 9

Views: 6232

Answers (2)

twalberg
twalberg

Reputation: 62369

Try eval "${run2}". That will interpret the variable as a sequence of commands to be run.

Upvotes: 2

favoretti
favoretti

Reputation: 30167

Try:

eval ${run2}

This should help.

Upvotes: 8

Related Questions