文章目录
类似于C/C++
中的include
操作,bash
也可以引入其他文件中的代码。
语法格式
. filename # 注意点和文件名之间有一个空格
或
source filename
示例
创建test1.sh
,内容为:
#! /bin/bash
name=Tom # 定义变量name
然后创建test2.sh
,内容为:
#! /bin/bash
source test1.sh # 或 . test1.sh
echo My name is: $name # 可以使用test1.sh中的变量
执行bash test2.sh
,输出My name is: Tom
。