和其他语言一样,Shell 也可以包含外部脚本。这样可以很方便的封装一些公用的代码作为一个独立的文件。
Shell 文件包含的语法格式如下:
. filename # 注意点号(.)和文件名中间有一空格
或
source filename
实例
创建两个 shell 脚本文件。
file.sh 代码如下:
#!/bin/bash
str="This is a testing."
test.sh 代码如下:
#!/bin/bash
. ./file.sh
echo $str
接下来,我们为 test.sh 添加可执行权限并执行:
$ chmod +x test.sh
$ ./test.sh
This is a testing.
参考自《菜鸟教程》