Importing shell script function

前端 未结 3 2159
Happy的楠姐
Happy的楠姐 2020-12-09 09:04

I have a function err() in file abc. The files do not have a .sh extension, but they start with #!/bin/bash.



        
相关标签:
3条回答
  • 2020-12-09 09:30

    You need to export newly created functions

    at end of abc add this:

    export -f err
    
    0 讨论(0)
  • 2020-12-09 09:31

    That's fine, here are some more hints:

    1. Use a naming convention for functions, for example prefix the function name with f_, for example f_err. Function calls appear no different as other commands, this is a hint to the reader. It also reduces the chances of a name collision.

    2. You need read access only, and you do not need the #!/bin/bash (its just a comment).

    3. In Bash, some options have to be set before function parsing. For example, shopt -s extglob has to be done before and outside the function if it uses extended globbing. Putting that inside the function is too late.

    4. Bash does not support the FPATH environment variable or autoload (as Korn shell does).

    0 讨论(0)
  • 2020-12-09 09:49

    Yes, you can do like you mentioned above or like: . FILENAME

    The file need not to end with .sh

    0 讨论(0)
提交回复
热议问题