Naveen
Naveen

Reputation: 159

Execute linux command from a variable

Here i am trying to execute linux command from a variable in file.sh.

test.sh

OUT= "date";
echo $OUT;

Output:
It is Working perfectly.

But when i try to execute the command pgrep vpnc

OUT= "pgrep vpnc";
echo $OUT;

Output
test.sh: 1: test.sh: pgrep vpnc: not found

my expectation when the above file is executed,it returns pid.

I also did tried by eval.

OUT= "pgrep vpnc";
$ eval $OUT;

Output:
test.sh: 1: test.sh: pgrep vpnc: not found
test.sh: 2: test.sh: $: not found

Can any one help me how to run command and store its value in a variable.

Any help is highly appreciated.

Upvotes: 1

Views: 1197

Answers (2)

Mali
Mali

Reputation: 2700

it shoud be ` instead of "

OUT=`pgrep process`;
echo $OUT;

display the pid of process.

Upvotes: 2

Explosion Pills
Explosion Pills

Reputation: 191749

Just using

$ $OUT

should run the command

Upvotes: 1

Related Questions