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
Why does a Star of David appear at a rally with Francisco Franco?
Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?
What is the purpose or proof behind chain rule?
Print a physical multiplication table
Math equation in non italic font
When to use a slotted vs. solid turner?
Are ETF trackers fundamentally better than individual stocks?
World War I as a war of liberals against authoritarians?
Is it good practice to use Linear Least-Squares with SMA?
Are Roman Catholic priests ever addressed as pastor
Why did it take so long to abandon sail after steamships were demonstrated?
ERC721: How to get the owned tokens of an address
Adventure Game (text based) in C++
How could an airship be repaired midflight?
Bacteria contamination inside a thermos bottle
Have the tides ever turned twice on any open problem?
Is there a symmetric-key algorithm which we can use for creating a signature?
How to write cleanly even if my character uses expletive language?
Is a party consisting of only a bard, a cleric, and a warlock functional long-term?
Happy pi day, everyone!
Why is a white electrical wire connected to 2 black wires?
A diagram about partial derivatives of f(x,y)
Could the Saturn V actually have launched astronauts around Venus?
How do I hide Chekhov's Gun?
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нιη
10 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 8 hours ago
ctrl-alt-delor
12k42360
12k42360
asked 11 hours ago
appleapple
274
274
2
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
10 hours ago
add a comment |
2
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
10 hours ago
2
2
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
10 hours ago
please also read stackoverflow.com/q/5228345/4023950
– αғsнιη
10 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
10 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
10 hours ago
Many thanks! Working now.
– apple
10 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
10 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
10 hours ago
Many thanks! Working now.
– apple
10 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
10 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
10 hours ago
Many thanks! Working now.
– apple
10 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 10 hours ago
answered 10 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
10 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
10 hours ago
Many thanks! Working now.
– apple
10 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
10 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
10 hours ago
Many thanks! Working now.
– apple
10 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
10 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
10 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
10 hours ago
Is there something I need to do in the second script to receive $variable1 ?
– apple
10 hours ago
Many thanks! Working now.
– apple
10 hours ago
Many thanks! Working now.
– apple
10 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нιη
10 hours ago