views:

38

answers:

1

Hi,

I'm not sure if its possible, but I'm looking for a way to call a bash function from its subprocess. It could be something like:

function testfunc() { echo test function; }
bash -c 'testfunc'

This doesn't work obviously, but is there any way to achieve something like this?

Thanks a lot for the help!

+3  A: 
$ function blargh () {
> echo $1
> }
$ export -f blargh
$ bash
$ blargh hi there
hi
$ exit
$ bash -c "blargh hi there"
hi

export -f is the non-obvious bit.

$ help export
export: export [-fn] [name[=value] ...] or export -p
    Set export attribute for shell variables.

    Marks each NAME for automatic export to the environment of subsequently
    executed commands.  If VALUE is supplied, assign VALUE before exporting.

    Options:
      -f    refer to shell functions
    ...
msw
Hey, thanks a lot!
Sahasranaman MS