Include Files in Bash
Similar to other programming languages which allow to include other files to a file, Bash scripting also allows to include (source) another shell script file to script file.
For example, to include config.sh script to current script file use following syntax, where config.sh is available in the same directory of the current script.
1 2 3 | #!/bin/bash source other-file.sh |
Include Shell Script in Other Shell Script
For this example, First, I have created a data.sh file with the following content.
1 2 3 4 | #!/bin/bash id=100 name="TecAdmin" |
Now create another file show.sh, which includes data.sh file.
1 2 3 4 5 6 | #!/bin/bash source data.sh echo "Welcome $name" echo "Your id is $id" |
Execute script show.sh in a terminal and view the results:
./show.sh Welcome TecAdmin Your id is 100