Skip to content

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 :

  1. call it from the shell
    Make the other script executable :  with chmod a+x /path/to/file
    add the #!/bin/bash line (called shebang) at the top,
    and the path where the file is to the $PATH environment variable.
    Then you can call a script asyou would a normal command;

  2. call it with the source command
     (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.