如何使用echo在execle()中打印环境变量?

时间:2016-12-26 12:26:34

标签: c unix system-calls

我试过这段代码。

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

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

它提供输出

$first $second $third

显然这不是我所期待的。他们是否可以使用echo打印环境变量?

我使用printenv获取变量。如果相关。

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

输出:

one
two
three

1 个答案:

答案 0 :(得分:2)

命令行扩展由shell在调用命令之前提供。为了按预期进行扩展,你可以使用" -c"来执行shell。选项和要执行的整个命令行。

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