3

Say I have a variable:

T=6

And another variable called 'line' that I read from a file and that contains the following string/text:

echo "$T"

Now I want to execute the command written in 'line' so I'll get:

6

Just like what I will get if will just type the command:

echo "$T"

And get:

6

I tried to use:

$line

But I got:

"$T"

Thanks in advance for the help.

1
  • Your variable line should be line=echo "$T". Then you can use echo $line. Commented Nov 21, 2014 at 11:24

1 Answer 1

2

eval is what you are looking for

For example consider

$ T=6
$ line='echo "$T"'
$ eval $line
6

From man pages

eval [arg ...]
              The  args  are  read  and concatenated together into a single command.  This command is then read and executed by the shell, and its exit
              status is returned as the value of eval.  If there are no args, or only null arguments, eval returns 0.
Sign up to request clarification or add additional context in comments.

1 Comment

Ob-link to Eval command and security issues "eval" is a common misspelling of "evil".

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.