webminal.org
webminal.org

Reputation: 47296

execute a command

I have script file where a command is stored in a variable

First i got the command (assume "ls -l " command)

cmd=`cat /proc/2345/cmdline`

now doing

echo $cmd

outputs

ls -l

Now how to use $cmd to actually execute that command. which is ls -l

Upvotes: 0

Views: 244

Answers (4)

Heinzi
Heinzi

Reputation: 172448

eval $cmd

Just $cmd will work in many cases, but not always. See the article "Bash: Why use eval with variable expansion?" for further details.

Upvotes: 3

ghostdog74
ghostdog74

Reputation: 342969

if you have no business to store it in a variable, then don't. Just run it as usual.

cat /proc/2345/cmdline

Upvotes: 2

Chen Levy
Chen Levy

Reputation: 16408

The simple answer is:

$cmd

Upvotes: 5

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799300

BASH FAQ entry #50.

Upvotes: 6

Related Questions