How to run a bash script in the same process as the calling script?

6,055

Solution 1

Alternatively, source the sub-script:

one.sh

#!/bin/sh

echo one.sh: pid is "$$"
. ./two.sh
echo done with "$0"

(the . command is exactly the same as source in bash, but . is more portable)

two.sh

echo two.sh: pid is "$$"

Sample run:

$ ./one.sh
one.sh: pid is 31290
two.sh: pid is 31290
done with ./one.sh

The script two.sh will be run in the same shell environment as one.sh, and the shell will not spawn a new process to run it. It behaves very much like calling a shell function, in more ways than one (for example, use return rather than exit to return control from two.sh to one.sh early; exit would exit the shell session completely).

Solution 2

If you don't need to run any other commands or do anything else after calling the sub-script, then you can use:

exec sub_script

The exec is a shell builtin which does:

$ help exec
exec: exec [-cl] [-a name] [command [arguments ...]] [redirection ...]
    Replace the shell with the given command.

    Execute COMMAND, replacing this shell with the specified program.
    ARGUMENTS become the arguments to COMMAND.  If COMMAND is not specified,
    any redirections take effect in the current shell.

    Options:
      -a name   pass NAME as the zeroth argument to COMMAND
      -c    execute COMMAND with an empty environment
      -l    place a dash in the zeroth argument to COMMAND

    If the command cannot be executed, a non-interactive shell exits, unless
    the shell option `execfail' is set.

    Exit Status:
    Returns success unless COMMAND is not found or a redirection error occurs.

So after running exec sub_script you have permanently left the parent script and cannot go back to it again.

Share:
6,055

Related videos on Youtube

user11276663
Author by

user11276663

Updated on September 18, 2022

Comments

  • user11276663
    user11276663 over 1 year

    I have a bash script that calls/invokes a sub-script with command-line arguments. How can I force the sub-script to run under the same PID as the caller?

  • nohillside
    nohillside about 5 years
    Well, at least as long as there are no additional commands after this line...
  • Gilles Quenot
    Gilles Quenot about 5 years
    Yes, sure, mandatory
  • Kusalananda
    Kusalananda about 5 years
    The issue with this one is that some sh shells (yash and dash) does not allow the . command to take more than a simple filename, so you can't pass command line arguments to the sourced dot-script. Bash, zsh and ksh, on the other hand, allows passing command line arguments to dot-scripts. In reality, passing arguments may not be needed though, as the dot-script is executing in the same environment and could therefore just use the variables it needs to use, just like a function would be able to do.
  • Jeff Schaller
    Jeff Schaller about 5 years
    Good clarifications and edits; thank you! I considered updating the Q's tags to include or be bash, given the Q's title and body.