Friday, April 29, 2016

How to call a shell script from another shell script

Note: Make sure to add #!/bin/bash to the first line of the script and make it executable through chmod 700 <path to script>.
  1. <path to script> or /bin/bash <path to script> or <scriptname> (add script to $PATH environment variable and call as normal command)
    - child script is executed in a new shell, changes in child script does not affect parent script
    - can be executed asynchronously through background command
  2. <path to script> &
    - same as no. 1 but executes child script on background
  3. source <path to script> or . <path to script>
    - child script is executed in a current shell
    - context is same as parent script (vars and functions of child script becomes visible on parent script)
    - exit call in child script will also exit the parent script
  4. exec <path to script>
    - terminates the current shell and executes on a new shell

No comments:

Post a Comment