Passing arguments from one script to another2019 Community Moderator ElectionPass command line arguments to bash scriptBash globbing and argument passingAdd arguments to 'bash -c'Passing arguments from one command into the nextHow to extract unknown arguments within a shell script?functions argumentsCall one shell script with anotherBASH: how to pass a default argument if no arguments after the first were passedBash script to pass arguments to a scriptHow to pass arguments to a script that were generated by another script
What do you call the act of removing a part of a word and replacing it with an apostrophe
gcc: how to detect bad `bool` usage
How to terminate ping <dest> &
What did “the good wine” (τὸν καλὸν οἶνον) mean in John 2:10?
What are substitutions for coconut in curry?
ERC721: How to get the owned tokens of an address
Instead of a Universal Basic Income program, why not implement a "Universal Basic Needs" program?
A diagram about partial derivatives of f(x,y)
What is a ^ b and (a & b) << 1?
Why no Iridium-level flares from other satellites?
Violin - Can double stops be played when the strings are not next to each other?
Do I need to be arrogant to get ahead?
What is the Japanese sound word for the clinking of money?
As a new Ubuntu desktop 18.04 LTS user, do I need to use ufw for a firewall or is iptables sufficient?
Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?
Time travel from stationary position?
Why does overlay work only on the first tcolorbox?
Why do passenger jet manufacturers design their planes with stall prevention systems?
Knife as defense against stray dogs
Have researchers managed to "reverse time"? If so, what does that mean for physics?
How to solve this challenging limit?
How to write cleanly even if my character uses expletive language?
How well should I expect Adam to work?
Python if-else code style for reduced code for rounding floats
Passing arguments from one script to another
2019 Community Moderator ElectionPass command line arguments to bash scriptBash globbing and argument passingAdd arguments to 'bash -c'Passing arguments from one command into the nextHow to extract unknown arguments within a shell script?functions argumentsCall one shell script with anotherBASH: how to pass a default argument if no arguments after the first were passedBash script to pass arguments to a scriptHow to pass arguments to a script that were generated by another script
Let's say I am in a Bash script and am sourcing a file. How do I pass variables to another script after I source the file like this.
sed -i 's/ = /=/' $file
source $file
Let's say file contains
variable1=10
variable2=apple
If I want to use these in another script, how do I pass these arguments to the other script, then run the script in my current Bash script.
bash scripting variable arguments
add a comment |
Let's say I am in a Bash script and am sourcing a file. How do I pass variables to another script after I source the file like this.
sed -i 's/ = /=/' $file
source $file
Let's say file contains
variable1=10
variable2=apple
If I want to use these in another script, how do I pass these arguments to the other script, then run the script in my current Bash script.
bash scripting variable arguments
2
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
4 hours ago
add a comment |
Let's say I am in a Bash script and am sourcing a file. How do I pass variables to another script after I source the file like this.
sed -i 's/ = /=/' $file
source $file
Let's say file contains
variable1=10
variable2=apple
If I want to use these in another script, how do I pass these arguments to the other script, then run the script in my current Bash script.
bash scripting variable arguments
Let's say I am in a Bash script and am sourcing a file. How do I pass variables to another script after I source the file like this.
sed -i 's/ = /=/' $file
source $file
Let's say file contains
variable1=10
variable2=apple
If I want to use these in another script, how do I pass these arguments to the other script, then run the script in my current Bash script.
bash scripting variable arguments
bash scripting variable arguments
edited 1 hour ago
ctrl-alt-delor
12k42360
12k42360
asked 4 hours ago
appleapple
274
274
2
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
4 hours ago
add a comment |
2
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
4 hours ago
2
2
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
4 hours ago
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
4 hours ago
add a comment |
1 Answer
1
active
oldest
votes
You would pass them pretty much the same as you would pass arguments in any other way:
sed -i 's/ = /=/' "$file"
source "$file"
/path/to/another/script.sh "$variable1" "$variable2"
Obviously using the appropriate command line switches (or not if applicable).
If using the code as above, the value of $variable1
will be available in the other script as $1
(the 1st command line argument), while $variable2
will be available as $2
.
To keep the original names in your new script you would need to reassign them using the positional parameters, ie:
variable1=$1
variable2=$2
However this may not be the most efficient way to do this, you might be better off with the suggestion below:
It sounds like you may actually want to source your file within the second script and not the first. In which case you may want to do the following:
script1.sh:
sed -i 's/ = /=/' "$file"
/path/to/another/script2.sh "$file"
script2.sh:
file=$1
source "$file"
printf '%sn' "$variable1"
printf '%sn' "$variable2"
Related recommended reading: 3.4.1 Positional Parameters
Note: assigning
$1
to thefile
variable is not necessary, you could also simplysource "$1"
but I have written it this way in an attempt to show how positional parameters are handled
I tried this (no necessary switches) and it's not working. The "another" script is running, because if I set the other script simply to "echo apple", it will display "apple", but if I set the other script to simply "echo $variable1", it displays a blank line. I know $variable1 has a value, because if I do "echo $variable1" before calling the other script, I get the proper value, so it seems that the variable is not getting passed.
– apple
4 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
4 hours ago
Many thanks! Working now.
– apple
3 hours ago
add a comment |
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
);
);
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%2f506695%2fpassing-arguments-from-one-script-to-another%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You would pass them pretty much the same as you would pass arguments in any other way:
sed -i 's/ = /=/' "$file"
source "$file"
/path/to/another/script.sh "$variable1" "$variable2"
Obviously using the appropriate command line switches (or not if applicable).
If using the code as above, the value of $variable1
will be available in the other script as $1
(the 1st command line argument), while $variable2
will be available as $2
.
To keep the original names in your new script you would need to reassign them using the positional parameters, ie:
variable1=$1
variable2=$2
However this may not be the most efficient way to do this, you might be better off with the suggestion below:
It sounds like you may actually want to source your file within the second script and not the first. In which case you may want to do the following:
script1.sh:
sed -i 's/ = /=/' "$file"
/path/to/another/script2.sh "$file"
script2.sh:
file=$1
source "$file"
printf '%sn' "$variable1"
printf '%sn' "$variable2"
Related recommended reading: 3.4.1 Positional Parameters
Note: assigning
$1
to thefile
variable is not necessary, you could also simplysource "$1"
but I have written it this way in an attempt to show how positional parameters are handled
I tried this (no necessary switches) and it's not working. The "another" script is running, because if I set the other script simply to "echo apple", it will display "apple", but if I set the other script to simply "echo $variable1", it displays a blank line. I know $variable1 has a value, because if I do "echo $variable1" before calling the other script, I get the proper value, so it seems that the variable is not getting passed.
– apple
4 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
4 hours ago
Many thanks! Working now.
– apple
3 hours ago
add a comment |
You would pass them pretty much the same as you would pass arguments in any other way:
sed -i 's/ = /=/' "$file"
source "$file"
/path/to/another/script.sh "$variable1" "$variable2"
Obviously using the appropriate command line switches (or not if applicable).
If using the code as above, the value of $variable1
will be available in the other script as $1
(the 1st command line argument), while $variable2
will be available as $2
.
To keep the original names in your new script you would need to reassign them using the positional parameters, ie:
variable1=$1
variable2=$2
However this may not be the most efficient way to do this, you might be better off with the suggestion below:
It sounds like you may actually want to source your file within the second script and not the first. In which case you may want to do the following:
script1.sh:
sed -i 's/ = /=/' "$file"
/path/to/another/script2.sh "$file"
script2.sh:
file=$1
source "$file"
printf '%sn' "$variable1"
printf '%sn' "$variable2"
Related recommended reading: 3.4.1 Positional Parameters
Note: assigning
$1
to thefile
variable is not necessary, you could also simplysource "$1"
but I have written it this way in an attempt to show how positional parameters are handled
I tried this (no necessary switches) and it's not working. The "another" script is running, because if I set the other script simply to "echo apple", it will display "apple", but if I set the other script to simply "echo $variable1", it displays a blank line. I know $variable1 has a value, because if I do "echo $variable1" before calling the other script, I get the proper value, so it seems that the variable is not getting passed.
– apple
4 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
4 hours ago
Many thanks! Working now.
– apple
3 hours ago
add a comment |
You would pass them pretty much the same as you would pass arguments in any other way:
sed -i 's/ = /=/' "$file"
source "$file"
/path/to/another/script.sh "$variable1" "$variable2"
Obviously using the appropriate command line switches (or not if applicable).
If using the code as above, the value of $variable1
will be available in the other script as $1
(the 1st command line argument), while $variable2
will be available as $2
.
To keep the original names in your new script you would need to reassign them using the positional parameters, ie:
variable1=$1
variable2=$2
However this may not be the most efficient way to do this, you might be better off with the suggestion below:
It sounds like you may actually want to source your file within the second script and not the first. In which case you may want to do the following:
script1.sh:
sed -i 's/ = /=/' "$file"
/path/to/another/script2.sh "$file"
script2.sh:
file=$1
source "$file"
printf '%sn' "$variable1"
printf '%sn' "$variable2"
Related recommended reading: 3.4.1 Positional Parameters
Note: assigning
$1
to thefile
variable is not necessary, you could also simplysource "$1"
but I have written it this way in an attempt to show how positional parameters are handled
You would pass them pretty much the same as you would pass arguments in any other way:
sed -i 's/ = /=/' "$file"
source "$file"
/path/to/another/script.sh "$variable1" "$variable2"
Obviously using the appropriate command line switches (or not if applicable).
If using the code as above, the value of $variable1
will be available in the other script as $1
(the 1st command line argument), while $variable2
will be available as $2
.
To keep the original names in your new script you would need to reassign them using the positional parameters, ie:
variable1=$1
variable2=$2
However this may not be the most efficient way to do this, you might be better off with the suggestion below:
It sounds like you may actually want to source your file within the second script and not the first. In which case you may want to do the following:
script1.sh:
sed -i 's/ = /=/' "$file"
/path/to/another/script2.sh "$file"
script2.sh:
file=$1
source "$file"
printf '%sn' "$variable1"
printf '%sn' "$variable2"
Related recommended reading: 3.4.1 Positional Parameters
Note: assigning
$1
to thefile
variable is not necessary, you could also simplysource "$1"
but I have written it this way in an attempt to show how positional parameters are handled
edited 3 hours ago
answered 4 hours ago
Jesse_bJesse_b
13.6k23371
13.6k23371
I tried this (no necessary switches) and it's not working. The "another" script is running, because if I set the other script simply to "echo apple", it will display "apple", but if I set the other script to simply "echo $variable1", it displays a blank line. I know $variable1 has a value, because if I do "echo $variable1" before calling the other script, I get the proper value, so it seems that the variable is not getting passed.
– apple
4 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
4 hours ago
Many thanks! Working now.
– apple
3 hours ago
add a comment |
I tried this (no necessary switches) and it's not working. The "another" script is running, because if I set the other script simply to "echo apple", it will display "apple", but if I set the other script to simply "echo $variable1", it displays a blank line. I know $variable1 has a value, because if I do "echo $variable1" before calling the other script, I get the proper value, so it seems that the variable is not getting passed.
– apple
4 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
4 hours ago
Many thanks! Working now.
– apple
3 hours ago
I tried this (no necessary switches) and it's not working. The "another" script is running, because if I set the other script simply to "echo apple", it will display "apple", but if I set the other script to simply "echo $variable1", it displays a blank line. I know $variable1 has a value, because if I do "echo $variable1" before calling the other script, I get the proper value, so it seems that the variable is not getting passed.
– apple
4 hours ago
I tried this (no necessary switches) and it's not working. The "another" script is running, because if I set the other script simply to "echo apple", it will display "apple", but if I set the other script to simply "echo $variable1", it displays a blank line. I know $variable1 has a value, because if I do "echo $variable1" before calling the other script, I get the proper value, so it seems that the variable is not getting passed.
– apple
4 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
4 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
4 hours ago
Many thanks! Working now.
– apple
3 hours ago
Many thanks! Working now.
– apple
3 hours ago
add a comment |
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%2f506695%2fpassing-arguments-from-one-script-to-another%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
2
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
4 hours ago