English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

包含文件Shell

和其他语言一样,Shell 也可以包含脚本外部。这样可以很方便的封装一些代码公用作为一个独立的文件。

格式语法包含文件Shell

. filename   # 注意点号(.)和文件名中间有一空格
或
source filename

示例在线

文件脚本两个shell创建。

代码如下:test1.sh

#!/bin/bash
# author:网教程基础
# url:it.oldtoolbag.com
url="http://it.oldtoolbag.com"

代码如下:test2.sh

#!/bin/bash
# author:网教程基础
# url:it.oldtoolbag.com
#使用 . 号来引用文件test1.sh
. ./test1.sh
# 或者使用以下包含文件代码
# source ./test1.sh
echo "地址官网网教程基础:$url"

接下来,我们为 test2.sh 添加权限可执行并执行:

$ chmod +x test2.sh 
$ ./test2.sh 
地址官网网教程基础:http://it.oldtoolbag.com

注:文件test1.sh包含不需要权限可执行。