Execute C program in Current shell

Hello,
I have a c program executable which I need to run inside a shell script.
But the c program runs in a subshell because of which all the actions done by the c program is not available to the current shell.

Is there any way to execute a C program binary executable in the current shell?

. ./a.out

You are joking, balajesuri, aren't you? :smiley:

There is no way to execute a compiled executable within a shell. Shells execute shell scripts, not binaries. If you need a side effect of a child process on the parent shell, your compiled program could output shell commands, which are intercepted and executed by the parent shell (sort of eval :slight_smile: )

For example, if the compiled program prints the following output to stdout:

ABC=123;
DEF=456;
cd /tmp;

and the shell starts the program like

eval $(./a.out)

the shell will define the two variables ABC and DEF and change the current working directory to /tmp.

If a C program needs to export some variables, things usually work the opposite way -- run your C program first, export a bunch of variables, then run your program. Your program will inherit the new values from it that way.

int main()
{
        setenv("FOO","BAR",1);
        execlp("/path/to/script.sh", "/path/to/script.sh", "arg1", "arg2", NULL);
        perror("Could not exec");
        exit(1);
}

You should explain why you want it to be run by the current shell.

In any case, if you have the source code of your program and if your shell is ksh93, there is a way to modify your program to make it a custom builtin command and then have it effectively running by the current shell, see www/ksh/builtins.mm mm document

If your only goal is to set variables, that would be overkill compared to the previous suggestions though.