Can a bash function be used in different scripts?

前端 未结 3 1539
孤独总比滥情好
孤独总比滥情好 2021-01-31 14:55

I\'ve got a function that I want to reference and use across different scripts. Is there any way to do this? I don\'t want to be re-writing the same function for different scrip

3条回答
  •  醉梦人生
    2021-01-31 15:46

    Yes, you can localize all your functions in a common file (or files). This is exactly what I do with all my utility functions. I have a single utility.shinc in my home directory that's used by all my programs with:

    . $HOME/utility.shinc
    

    which executes the script in the context of the current shell. This is important - if you simply run the include script, it will run in a subshell and any changes will not propagate to the current shell.

    You can do the same thing for groups of scripts. If it's part of a "product", I'd tend to put all the scripts, and any included scripts, in a single shell directory to ensure everything is localized.

提交回复
热议问题