Set a Bash function on the environment

5.8k Views Asked by At

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

2

There are 2 best solutions below

0
rici On BEST ANSWER

If you are invoking bash with execv (so that you are only invoking it once), you could replace (using execl for explanatory purposes):

 execl("/bin/bash", "bash", "file_to_run", "arg1", "arg2", 0);

with

 execl("/bin/bash", "bash", "-c", "f() {...} g() {...}\n. $0",
                    "file_to_run", "arg1", "arg2", 0);

and then you don't need to play games with the internal bash interface for defining functions. (If the bash script being run also needs the functions to be exported, for whatever reason, just add export -f <func> lines to the argument following -c.)

That has the advantage of working with both patched and unpatched bashes.

(I'm having to make a similar patch to various programs, so I share your pain.)

2
chepner On

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);