Shell 也可以包含外部腳本肾砂。這樣可以很方便的封裝一些公用的代碼作為一個獨立的文件办素。
Shell 文件包含的語法格式如下:
. filename # 注意點號(.)和文件名中間有一空格 , filename文件的路徑
或
source filename
示例如下
創(chuàng)建兩個 shell 腳本文件读宙。
test1.sh 代碼如下:
#!/bin/bash
url="http://www.runoob.com"
test2.sh 代碼如下
#!/bin/bash
#使用 . 號來引用test1.sh 文件
. ./test1.sh
# 或者使用以下包含文件代碼
# source ./test1.sh
echo "網地址:$url"
接下來百侧,我們?yōu)?test2.sh 添加可執(zhí)行權限并執(zhí)行:
$ chmod +x test2.sh
$ ./test2.sh
網地址:http://www.runoob.com
注:被包含的文件 test1.sh 不需要可執(zhí)行權限版姑。