Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I want to write a c program with some shell scripts. Now For a simple C program. I am Setting a variable called val2 in bash, now I want ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    5

    How To Use Bash Variable In C Code?


    Hi,
    I want to write a c program with some shell scripts.
    Now For a simple C program. I am Setting a variable called val2 in bash, now I want to use bash variable val2 in C code. How do I do that?
    Here's my code :-

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
            system("export val2; val2=1234");
    
            printf ("Val2 Value - %s\n", getenv("val2"));
    
            return 0;
    }
    The above doesn't work (coz its spawning a different memory space and when shell script ends the variable dies with it as per my research but how do I keep them in same memory space)

    Also Is there any Good reference where they teach how to integrate C and Bash Together?

    Thanks.

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,226
    Well, either you can have bash pass it to your program as an argument, or you can export it (from bash) to the environment and then use the getenv() function to read it back.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    You cannot do this directly.

    When you run a command using "system", you actually create a subprocess that runs the command. It is impossible for a child process to modify its parent's environment.

    What you can do instead is something called IPC (Inter-process Communication) to somehow communicate between the two, and the parent can choose what to do with that information.

    So in this particular case, you could have the child process write a value to a file, and the parent process then reads that file to find out what it should set val2 to.

    A more common way of doing this would be, instead of using "system", to use "popen" to allow the child process's output to be easily read by the parent. This is a convenient wrapper around the pipe()/fork()/exec() paradigm.

  4. #4
    Just Joined!
    Join Date
    Jan 2011
    Posts
    5
    Can you show me a how for this example I could have used popen()?

    Thanks.

  5. #5
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    1,226
    Well, it's tricky if you want to use bash. The syntax of popen() is
    Code:
    Stream = popen(command, mode)
    .
    In your case, the mode will be "r" as you want the stream to be read from the child's standard output. But to make bash output the value, you would either have to run a bash script containing echo commands or use the -c option to run an echo command from the command line.

    System() and popen() expect their commands to be a single string; when I use system() to launch a child, I use sprintf() first to put the command and arguments (including variable arguments) into a string, then pass the string to system(). Maybe Cabhan knows a simpler way to do it.
    "I'm just a little old lady; don't try to dazzle me with jargon!"
    www.hrussman.entadsl.com

  6. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    A fuller example program is:
    Code:
    alex@niamh:~/test/c$ cat popen_bash.c 
    #include <stdio.h>
    
    int main() {
        char buf[BUFSIZ];
    
        FILE *pipe = popen("echo hello", "r");
    
        fgets(buf, BUFSIZ, pipe);
    
        printf("Stream outputted: %s\n", buf);
    
        return 0;
    }
    alex@niamh:~/test/c$ ./a.out 
    Stream outputted: hello
    As hazel says, you will need to construct a single string as the command to run. My preferred way to do this is to simply write a Bash script that you can call from your C program, but you could also theoretically construct a Bash commandline somehow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •