Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a Bash function on the environment

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

like image 329
JoseLSegura Avatar asked Jan 09 '23 15:01

JoseLSegura


1 Answers

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);
like image 55
chepner Avatar answered Jan 18 '23 02:01

chepner