Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Shell File Inclusion

Shell File Inclusion

Like other languages, Shell can also include external scripts. This makes it convenient to encapsulate some common code as a separate file.

The syntax for Shell file inclusion is as follows:



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



或



source filename

Example

Create two shell script files.

The test1.sh code is as follows:



#!/bin/bash

# author:Runoob Tutorial

# url:www.runoob.com



url="http://www.runoob.com"

The test2.sh code is as follows:



#!/bin/bash

# author:Runoob Tutorial

# url:www.runoob.com



#Use . to source test1.sh file

. ./test1.sh



# Or use the following included file code

# source ./test1.sh



echo "Runoob Tutorial官网地址:$url"

Next, we add executable permission to test2.sh and run it:



$ chmod +x test2.sh 

$ ./test2.sh 

Runoob Tutorial官网地址:http://www.runoob.com

Note:The included file test1.sh does not need executable permission.