I need to define a Bash function in the Bash environment from a C/C++ program. Before the shellshock bug, I could define a function in this way:
my_func='() { echo "This is my function";}'
Or equivalent from a C program:
setenv("my_func", "() { echo \"This is my function\";}", 1);
Or
putenv("my_func=() { echo \"This is my function\";}");
But using a Bash version with shellshock fixed, I can't manage on how to define my functions in the environment.
The strange thing is, if I run env
, I can see my function defined in the environment, but if I call it, Bash says that it doesn't exist.
Thanks in advance
For informational purposes only. Since it is not documented how functions are exported to the environment, you should treat this as an abuse of a private API that is subject to change in future versions of bash
.
Functions are no longer exported using simply the name of the function in the environment string. To see this, run
$ my_func () { echo "foo"; }
$ export -f my_func
$ env | grep -A1 'my_func'
BASH_FUNC_my_func%%=() { echo "foo"
}
Since the name used in the environment is no longer a valid bash
identifier, you would need to use the env
command to modify the environment of the new process.
env 'BASH_FUNC_my_func%%=() { echo "This is my function"; }' bash
From C, you just need to adjust the name.
setenv("BASH_FUNC_my_func%%", "() { echo \"This is my function\";}", 1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With