9

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

2 Answers 2

8

Try:

eval ${run2}

This should help.

Sign up to request clarification or add additional context in comments.

2 Comments

it works... maybe i am inquisitive, but can you explain why it works for run1 but not for run2...
Because it is quoted, the semi-colon is part of the first word after quote removal, not a syntactic element to separate two commands.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.