Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling bash functions from sub process

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!

like image 858
Sahas Avatar asked Jun 28 '26 04:06

Sahas


1 Answers

$ 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
    ...
like image 76
msw Avatar answered Jun 29 '26 17:06

msw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!