Shell 檔案包含
和其他語言一樣,Shell 也可以包含外部指令碼。這樣可以很方便的封裝一些公用的程式碼作為一個獨立的檔案。
Shell 檔案包含的語法格式如下:
. filename # 注意點號 (.) 和檔名中間有一空格

source filename
例項
建立兩個 shell 指令碼檔案。
test1.sh 程式碼如下:
#!/bin/bash
# author: 菜鳥教程
# url:www.runoob.com

url=”http://www.runoob.com”
test2.sh 程式碼如下:
#!/bin/bash
# author: 菜鳥教程
# url:www.runoob.com

#使用 . 號來引用 test1.sh 檔案
. ./test1.sh

# 或者使用以下包含檔案程式碼
# source ./test1.sh

echo “ 菜鳥教程官網地址:$url”
接下來,我們為 test2.sh 新增可執行許可權並執行:
$ chmod +x test2.sh
$ ./test2.sh
菜鳥教程官網地址:http://www.runoob.com
注:被包含的檔案 test1.sh 不需要可執行許可權。
原文連結:http://www.runoob.com/linux/linux-shell-include-file.html