How to run a bash script in the same process as the calling script? The Next CEO of Stack Overflow“history” stops working when run inside bash scriptHow to get the PID of a sub process across to the parent process in a bash shell script?How to run a bash script from a sh(dash) script?Subshell for Bash ScriptGet PID of the host script in bashBash script inheritance? Calling a function from another script?How can I prevent from script from dying if a process ID doesn't exist?How can I make a progress bar for a shell script calling an executable?How to make my bash function known to external programBash: Complete wrapper for command line programs

Is it ok to trim down a tube patch?

Airplane gently rocking its wings during whole flight

Is there a way to save my career from absolute disaster?

Could a dragon use its wings to swim?

Is there such a thing as a proper verb, like a proper noun?

Why did early computer designers eschew integers?

Can this note be analyzed as a non-chord tone?

What was Carter Burke's job for "the company" in Aliens?

Yu-Gi-Oh cards in Python 3

What is the process for cleansing a very negative action

Is there an equivalent of cd - for cp or mv

Is it correct to say moon starry nights?

free fall ellipse or parabola?

Players Circumventing the limitations of Wish

Can you teleport closer to a creature you are Frightened of?

Computationally populating tables with probability data

Is it OK to decorate a log book cover?

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact

Physiological effects of huge anime eyes

Is it okay to majorly distort historical facts while writing a fiction story?

How to Implement Deterministic Encryption Safely in .NET

Help! I cannot understand this game’s notations!

It is correct to match light sources with the same color temperature?

Reference request: Grassmannian and Plucker coordinates in type B, C, D



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



The Next CEO of Stack Overflow“history” stops working when run inside bash scriptHow to get the PID of a sub process across to the parent process in a bash shell script?How to run a bash script from a sh(dash) script?Subshell for Bash ScriptGet PID of the host script in bashBash script inheritance? Calling a function from another script?How can I prevent from script from dying if a process ID doesn't exist?How can I make a progress bar for a shell script calling an executable?How to make my bash function known to external programBash: Complete wrapper for command line programs










0















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?










share|improve this question









New contributor




user11276663 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    0















    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?










    share|improve this question









    New contributor




    user11276663 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      0












      0








      0








      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?










      share|improve this question









      New contributor




      user11276663 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      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?







      bash shell-script process






      share|improve this question









      New contributor




      user11276663 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      user11276663 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 days ago









      Jeff Schaller

      44.4k1162143




      44.4k1162143






      New contributor




      user11276663 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      user11276663user11276663

      1




      1




      New contributor




      user11276663 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user11276663 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user11276663 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes


















          3














          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).






          share|improve this answer




















          • 1





            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.

            – Kusalananda
            2 days ago












          • 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.

            – Jeff Schaller
            2 days ago



















          1














          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|improve this answer

























          • Well, at least as long as there are no additional commands after this line...

            – nohillside
            2 days ago











          • Yes, sure, mandatory

            – Gilles Quenot
            2 days ago











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          user11276663 is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f509467%2fhow-to-run-a-bash-script-in-the-same-process-as-the-calling-script%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          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).






          share|improve this answer




















          • 1





            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.

            – Kusalananda
            2 days ago












          • 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.

            – Jeff Schaller
            2 days ago
















          3














          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).






          share|improve this answer




















          • 1





            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.

            – Kusalananda
            2 days ago












          • 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.

            – Jeff Schaller
            2 days ago














          3












          3








          3







          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).






          share|improve this answer















          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).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago









          Kusalananda

          139k17259429




          139k17259429










          answered 2 days ago









          Jeff SchallerJeff Schaller

          44.4k1162143




          44.4k1162143







          • 1





            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.

            – Kusalananda
            2 days ago












          • 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.

            – Jeff Schaller
            2 days ago













          • 1





            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.

            – Kusalananda
            2 days ago












          • 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.

            – Jeff Schaller
            2 days ago








          1




          1





          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.

          – Kusalananda
          2 days ago






          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.

          – Kusalananda
          2 days ago














          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.

          – Jeff Schaller
          2 days ago






          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.

          – Jeff Schaller
          2 days ago














          1














          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|improve this answer

























          • Well, at least as long as there are no additional commands after this line...

            – nohillside
            2 days ago











          • Yes, sure, mandatory

            – Gilles Quenot
            2 days ago















          1














          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|improve this answer

























          • Well, at least as long as there are no additional commands after this line...

            – nohillside
            2 days ago











          • Yes, sure, mandatory

            – Gilles Quenot
            2 days ago













          1












          1








          1







          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|improve this answer















          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|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago









          terdon

          133k33266446




          133k33266446










          answered 2 days ago









          Gilles QuenotGilles Quenot

          16.4k14053




          16.4k14053












          • Well, at least as long as there are no additional commands after this line...

            – nohillside
            2 days ago











          • Yes, sure, mandatory

            – Gilles Quenot
            2 days ago

















          • Well, at least as long as there are no additional commands after this line...

            – nohillside
            2 days ago











          • Yes, sure, mandatory

            – Gilles Quenot
            2 days ago
















          Well, at least as long as there are no additional commands after this line...

          – nohillside
          2 days ago





          Well, at least as long as there are no additional commands after this line...

          – nohillside
          2 days ago













          Yes, sure, mandatory

          – Gilles Quenot
          2 days ago





          Yes, sure, mandatory

          – Gilles Quenot
          2 days ago










          user11276663 is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          user11276663 is a new contributor. Be nice, and check out our Code of Conduct.












          user11276663 is a new contributor. Be nice, and check out our Code of Conduct.











          user11276663 is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f509467%2fhow-to-run-a-bash-script-in-the-same-process-as-the-calling-script%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

          Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

          Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.