You can transfer the value from your program to the shell via stdout (as already mentioned) or using the return in your main() function from your C program. Below is one liner that illustrates both approaches:
echo -e '#include <stdio.h>\n int main() { int a=11; int b=22; printf("%d\\n", a); return b; }' | gcc -xc -; w=$(./a.out); echo $?; echo $w
Output:
22
11
The variable a is printed on stdout , and the variable b returned in main() . Use $? in bash to get the return value of the most recently invoked command (in this case ./a.out ).
source share