The shell file contains
Like other languages, shells can also contain external scripts. This makes it easy to encapsulate some common code as a stand-alone file.
The shell file contains the following syntax format:
. filename # 注意点号(.)和文件名中间有一空格 或 source filename
Instance
Create two shell script files.
test1.sh code is as follows:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn url="http://www.w3cschool.cn"
test2.sh code is as follows:
#!/bin/bash # author:W3Cschool教程 # url:www.w3cschool.cn #使用 . 号来引用test1.sh 文件 . ./test1.sh # 或者使用以下包含文件代码 # source ./test1.sh echo "W3Cschool教程官网地址:$url"
Next, we add test2.sh permissions to the file and execute:
$ chmod +x test2.sh $ ./test2.sh W3Cschool教程官网地址:http://www.w3cschool.cn
Note: The contained file test1.sh requires executable permissions.