How to include file in a bash shell script

后端 未结 5 852
不知归路
不知归路 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:35

    In my situation, in order to include color.sh from the same directory in init.sh, I had to do something as follows.

    . ./color.sh
    

    Not sure why the ./ and not color.sh directly. The content of color.sh is as follows.

    RED=`tput setaf 1`
    GREEN=`tput setaf 2`
    BLUE=`tput setaf 4`
    BOLD=`tput bold`
    RESET=`tput sgr0`
    

    Making use of File color.sh does not error but, the color do not display. I have tested this in Ubuntu 18.04 and the Bash version is:

    GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)

提交回复
热议问题