How to call shell script from another shell script?

I have two shell scripts, a.sh and b.sh .

How can I call b.sh from within the shell script a.sh ?


There are a couple of ways you can do this:

  • Make the other script executable, add the #!/bin/bash line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command.

  • Call it with the source command (alias is . ) like this: source /path/to/script .

  • Use the bash command to execute it: /bin/bash /path/to/script .

  • The first and third methods execute the script as another process, so variables and functions in the other script will not be accessible.
    The second method 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.


    看一下这个。

    #!/bin/bash
    echo "This script is about to run another script."
    sh ./script.sh
    echo "This script has just run another script."
    

    The answer which I was looking for:

    ( exec "path/to/script" )
    

    As mentioned, exec replaces the shell without creating a new process. However , we can put it in a subshell, which is done using the parantheses.

    EDIT: Actually ( "path/to/script" ) is enough.

    链接地址: http://www.djcxy.com/p/56854.html

    上一篇: 我怎样才能让`find`忽略.svn目录?

    下一篇: 如何从另一个shell脚本调用shell脚本?