[Shell详解-10]:引入外部脚本

Shell语法详解目录

文章目录

类似于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

猜你喜欢

转载自blog.csdn.net/weixin_52341477/article/details/127614410