Passing arguments from one script to another The Next CEO of Stack OverflowPass command line...

How to count occurrences of text in a file?

How do I transpose the first and deepest levels of an arbitrarily nested array?

How do I reset passwords on multiple websites easily?

Several mode to write the symbol of a vector

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

Why do airplanes bank sharply to the right after air-to-air refueling?

Would a completely good Muggle be able to use a wand?

sp_blitzCache results Memory grants

Why do professional authors make "consistency" mistakes? And how to avoid them?

Received an invoice from my ex-employer billing me for training; how to handle?

Can I run my washing machine drain line into a condensate pump so it drains better?

Won the lottery - how do I keep the money?

Is there a difference between "Fahrstuhl" and "Aufzug"

What benefits would be gained by using human laborers instead of drones in deep sea mining?

Can you replace a racial trait cantrip when leveling up?

Is micro rebar a better way to reinforce concrete than rebar?

Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?

Contours of a clandestine nature

What's the best way to handle refactoring a big file?

Is it possible to search for a directory/file combination?

Can we say or write : "No, it'sn't"?

What happened in Rome, when the western empire "fell"?

Is it ever safe to open a suspicious html file (e.g. email attachment)?



Passing arguments from one script to another



The Next CEO of Stack OverflowPass 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












1















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.










share|improve this question




















  • 2





    please also read stackoverflow.com/q/5228345/4023950

    – αғsнιη
    Mar 16 at 16:19
















1















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.










share|improve this question




















  • 2





    please also read stackoverflow.com/q/5228345/4023950

    – αғsнιη
    Mar 16 at 16:19














1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 16 at 18:41









ctrl-alt-delor

12.2k42561




12.2k42561










asked Mar 16 at 16:01









appleapple

274




274








  • 2





    please also read stackoverflow.com/q/5228345/4023950

    – αғsнιη
    Mar 16 at 16:19














  • 2





    please also read stackoverflow.com/q/5228345/4023950

    – αғsнιη
    Mar 16 at 16:19








2




2





please also read stackoverflow.com/q/5228345/4023950

– αғsнιη
Mar 16 at 16:19





please also read stackoverflow.com/q/5228345/4023950

– αғsнιη
Mar 16 at 16:19










1 Answer
1






active

oldest

votes


















3














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 the file variable is not necessary, you could also simply source "$1" but I have written it this way in an attempt to show how positional parameters are handled







share|improve this answer


























  • 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
    Mar 16 at 16:29











  • Is there something I need to do in the second script to receive $variable1 ?

    – apple
    Mar 16 at 16:30











  • Many thanks! Working now.

    – apple
    Mar 16 at 16:40












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
});


}
});














draft saved

draft discarded


















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









3














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 the file variable is not necessary, you could also simply source "$1" but I have written it this way in an attempt to show how positional parameters are handled







share|improve this answer


























  • 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
    Mar 16 at 16:29











  • Is there something I need to do in the second script to receive $variable1 ?

    – apple
    Mar 16 at 16:30











  • Many thanks! Working now.

    – apple
    Mar 16 at 16:40
















3














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 the file variable is not necessary, you could also simply source "$1" but I have written it this way in an attempt to show how positional parameters are handled







share|improve this answer


























  • 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
    Mar 16 at 16:29











  • Is there something I need to do in the second script to receive $variable1 ?

    – apple
    Mar 16 at 16:30











  • Many thanks! Working now.

    – apple
    Mar 16 at 16:40














3












3








3







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 the file variable is not necessary, you could also simply source "$1" but I have written it this way in an attempt to show how positional parameters are handled







share|improve this answer















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 the file variable is not necessary, you could also simply source "$1" but I have written it this way in an attempt to show how positional parameters are handled








share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 16 at 16:49

























answered Mar 16 at 16:10









Jesse_bJesse_b

14.1k23572




14.1k23572













  • 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
    Mar 16 at 16:29











  • Is there something I need to do in the second script to receive $variable1 ?

    – apple
    Mar 16 at 16:30











  • Many thanks! Working now.

    – apple
    Mar 16 at 16:40



















  • 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
    Mar 16 at 16:29











  • Is there something I need to do in the second script to receive $variable1 ?

    – apple
    Mar 16 at 16:30











  • Many thanks! Working now.

    – apple
    Mar 16 at 16:40

















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
Mar 16 at 16:29





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
Mar 16 at 16:29













Is there something I need to do in the second script to receive $variable1 ?

– apple
Mar 16 at 16:30





Is there something I need to do in the second script to receive $variable1 ?

– apple
Mar 16 at 16:30













Many thanks! Working now.

– apple
Mar 16 at 16:40





Many thanks! Working now.

– apple
Mar 16 at 16:40


















draft saved

draft discarded




















































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%2f506695%2fpassing-arguments-from-one-script-to-another%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

Nidaros erkebispedøme

Birsay

Where did Arya get these scars? Unicorn Meta Zoo #1: Why another podcast? Announcing the arrival of Valued Associate #679: Cesar Manara Favourite questions and answers from the 1st quarter of 2019Why did Arya refuse to end it?Has the pronunciation of Arya Stark's name changed?Has Arya forgiven people?Why did Arya Stark lose her vision?Why can Arya still use the faces?Has the Narrow Sea become narrower?Does Arya Stark know how to make poisons outside of the House of Black and White?Why did Nymeria leave Arya?Why did Arya not kill the Lannister soldiers she encountered in the Riverlands?What is the current canonical age of Sansa, Bran and Arya Stark?