Execute a shell script from within another calling shell script
execute a shell script from within another calling shell script
different ways to do this :
-
call it from the shell
Make the other script executable : withchmod a+x /path/to/file
add the#!/bin/bashline (called shebang) at the top,
and the path where the file is to the$PATHenvironment variable.
Then you can call a script asyou would a normal command; -
call it with the
sourcecommand
(which is an alias for.)
source /path/to/script
```
3. use the `bash` command
with the shel filename as parameter
```bash
/bin/bash /path/to/script
The first and third approaches execute the script as another process, so variables and functions in the other script will not be accessible.
The second approach executes the script in the first script’s process, and pulls in variables and functions from the other script (so they are usable from the calling script).
In the second method, if you are using exit in second script, it will exit the first script as well. Which will not happen in first and third methods.