代码之家  ›  专栏  ›  技术社区  ›  Sahas

从子进程调用bash函数

  •  3
  • Sahas  · 技术社区  · 16 年前

    我不确定这是否可行,但我正在寻找一种从其子进程调用bash函数的方法。可能是这样的:

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

    非常感谢你的帮助!

    1 回复  |  直到 16 年前
        1
  •  4
  •   msw    16 年前
    $ function blargh () {
    > echo $1
    > }
    $ export -f blargh
    $ bash
    $ blargh hi there
    hi
    $ exit
    $ bash -c "blargh hi there"
    hi
    

    export -f 是不明显的一点。

    $ 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
        ...