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
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
New contributor
add a comment |
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
New contributor
add a comment |
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
New contributor
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
bash shell-script process
New contributor
New contributor
edited 2 days ago
Jeff Schaller♦
44.4k1162143
44.4k1162143
New contributor
asked 2 days ago
user11276663user11276663
1
1
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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).
1
The issue with this one is that somesh
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
add a comment |
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.
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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).
1
The issue with this one is that somesh
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
add a comment |
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).
1
The issue with this one is that somesh
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
add a comment |
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).
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).
edited 2 days ago
Kusalananda♦
139k17259429
139k17259429
answered 2 days ago
Jeff Schaller♦Jeff Schaller
44.4k1162143
44.4k1162143
1
The issue with this one is that somesh
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
add a comment |
1
The issue with this one is that somesh
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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