Calling bash functions from a subprocess

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

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

This does not work explicitly, but is there a way to achieve something like this?

Thank you very much for your help!

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

export -f - 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
    ...
+4
source

Source: https://habr.com/ru/post/1758150/


All Articles