C function call in bash script

Related to question 3451993 , can you call a function that is internal to subst.c (in the Bash source code) in a Bash script?

+6
c bash
source share
5 answers

Bash supports downloadable built-in features. You could use this to do what you want. See the files in the /usr/share/doc/bash/examples/loadables (or similar) directory.

+5
source share

Not.

You cannot access the inner function for a shell shell from a shell unless it is exported as a shell function.

+3
source share

The easiest way to do this is to write a simple program that collects the input, passes it to the function, and then prints the result. Why don't you tell us what you are trying to accomplish, and perhaps we can offer an easier way to "trick this cat."

+1
source share

No, you have to write a short C program, compile it and invoke it from the shell.

0
source share

This code looks pretty elegant: (from here ) The same solution @Jay pointed out.

 bash$ cat testing.c #include <stdio.h> char* say_hello() { return "hello world"; } float calc_xyzzy() { return 6.234; } int main(int argc, char** argv) { if (argc>1) { if (argv[1][0] =='1') { fprintf(stdout,"%s\n",say_hello()); } else if ( argv[1][0] == '2') { fprintf(stdout,"%g\n",calc_xyzzy()); } } return 0; } bash$ gcc -o testing testing.c bash$ ./testing 1 hello world bash$ ./testing 2 6.234 bash$ var_1="$(./testing 1)" bash$ var_2="$(./testing 2)" bash$ echo $var_1 hello world bash$ echo $var_2 6.234 bash$ 
0
source share

All Articles