Yogesh
Yogesh

Reputation: 25

How to Print environment variable in execle() using echo?

I tried this code.

char *env[]={"first=one","second=two","third=three",NULL};

execle("/bin/echo","echo","$first","$second","$third",(char *)0,env);

It gives output

$first $second $third

Clearly this is not what I am expecting. Is their any way to print environment variables using echo?

I gets the variables using printenv. If it relates.

 execle("/usr/bin/printenv","printenv","first","second","third",(char *)0,env);

Output:

one
two
three

Upvotes: 1

Views: 461

Answers (1)

Marian
Marian

Reputation: 7472

Command line expansions are provided by shell before the command is called. To get the expansion as expected you can exec shell with "-c" option and whole command line to be executed.

execle("/bin/bash","bash", "-c", "echo $first $second $third",(char *)0,env);

Upvotes: 3

Related Questions