Forwarding return value of shell script to C/C++ [closed]

I tried to find possible solution on Google, but all I found is explanation on how to return value, but not how to accept it in some programming language, in my case C/C++? Does anybody have idea on how to do this? Some tips?


I’m focussing on Linux, but my answer probably fits for other POSIX systems.

You’ll execute your shell script from a C or C++ program with some of:

  • the usual mixture of fork(2), execve(2), waitpid(2), and then waitpid gives you the exit code using WEXITSTATUS when  WIFEXITED

  • the system(3) C standard library function. It returns what the internal waipid implementing it is returning

  • the popen(3) POSIX standard library function. Use WEXITSTATUS on the result of pclose

Both system & popen are using the fork, execve, waitpid system calls (and some others, see syscalls(2) for a list).

Read any good book on Linux system programming for details (e.g. Advanced Linux Programming by M.Mitchell et al; you’ll find copies on the Web, or APUE etc…)

Leave a Reply

Your email address will not be published. Required fields are marked *