Easy Tutorial
❮ Linux Comm Mkfs Linux Comm Tmpwatch ❯

Shell File Inclusion

Like other languages, Shell can also include external scripts. This allows for convenient encapsulation of common code into a separate file.

The syntax for Shell file inclusion is as follows:

. filename   # Note the space between the dot (.) and the filename

or

source filename

Example

Create two shell script files.

The code for test1.sh is as follows:

#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org

url="http://www.tutorialpro.org"

The code for test2.sh is as follows:

#!/bin/bash
# author:tutorialpro.org
# url:www.tutorialpro.org

# Use the dot (.) to reference the `test1.sh` file
. ./test1.sh

# Alternatively, use the following to include the file
# source ./test1.sh

echo "tutorialpro.org website address: $url"

Next, we add executable permissions to test2.sh and execute it:

$ chmod +x test2.sh 
$ ./test2.sh 
tutorialpro.org website address: http://www.tutorialpro.org

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

❮ Linux Comm Mkfs Linux Comm Tmpwatch ❯