Im doing a testing for taking the previous values of dbgname and export the values to the new variable (CDUMAPDBGNAME_NEW), but it doesn't output the value when i do the echo $CDUMAPDBGNAME_NEW statement. Anyone can help me on this?

#include <stdio.h>
#include <string.h>

main (int argc, char **argv)

char dbgname [300];
char g_route [3+1];
char g_area [3];
char command[300];
int status;
strcpy (g_route, "1000");
strcpy (g_area, "AAA");

sprintf(dbgname,"mobileup.%s.%s.debug", g_route, g_area);
//printf("mobileup.%s.%s.debug", "1000","AAA");
sprintf(command,"export CDUMAPDBGNAME_NEW=%s",dbgname);
status=system (command);
    return 0;


Recommended Answers

All 2 Replies

The problem with your approach (and the one subith86 suggests) is that the environment variables you are setting are valid only for the shell they are executed in. So when you call system or putenv you are respectively writing to a new shell that goes away after the system call or writing to the environment hosting your executable (which also goes away when your executable does).

To have a persistent environment variable you need to have that variable loaded when the shell is created (ala .bashrc ) otherwise you can only expect to use the changes you make while you are still in the current shell.

Note that if all you want is changes to be valid during your program execution then the code subith86 provided is sufficient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.