How to include file in a bash shell script

后端 未结 5 857
不知归路
不知归路 2021-01-29 23:53

Is there a way to include another shell script in a shell script to be able to access its functions?

Like how in PHP you can use the include directive with

5条回答
  •  甜味超标
    2021-01-30 00:39

    Above answers are correct, but if run script in other folder, there will be some problem.

    For example, the a.sh and b.sh are in same folder, a include b with . ./b.sh to include.

    When run script out of the folder, for example with xx/xx/xx/a.sh, file b.sh will not found: ./b.sh: No such file or directory.

    I use

    . $(dirname "$0")/b.sh
    

提交回复
热议问题