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

Shell File Inclusion

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

The syntax format of Shell file inclusion is as follows:

. filename   # Note that there is a space between the dot and the filename
or
source filename

Online Examples

Create two shell script files.

test1.sh code as follows:

#!/bin/bash
# author:Basic Tutorial Network
# url:www.oldtoolbag.com
url="http://www.oldtoolbag.com"

test2.sh code as follows:

#!/bin/bash
# author:Basic Tutorial Network
# url:www.oldtoolbag.com
#Use the . symbol to refer to test1.sh file
. ./test1.sh
# or use the following included file code
# source ./test1.sh
echo "Official website address of Basic Tutorial Network: $url"

Next, we will set up the test2Add executable permission to .sh and execute:

$ chmod +x test2.sh 
$ ./test2.sh 
Official website address of Basic Tutorial Network: http://www.oldtoolbag.com

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