How do I execute a string command in a variable?

Let's say I have a variable:

T=6

      

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

echo "$T"

      

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

6

      

Like what I get, just enter the command:

echo "$T"

      

And we get:

6

      

I tried using:

$line

      

But I got:

"$T"

      

Thanks in advance for your help.

+3


source to share


1 answer


eval

is what you are looking for

For example consider

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

      



From the 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.

      

+2


source







All Articles