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










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нιη
    10 hours ago















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нιη
    10 hours ago













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












  • 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










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










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















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













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

















  • 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

















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

Oświęcim Innehåll Historia | Källor | Externa länkar | Navigeringsmeny50°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.2213950°2′18″N 19°13′17″Ö / 50.03833°N 19.22139°Ö / 50.03833; 19.221393089658Nordisk familjebok, AuschwitzInsidan tro och existensJewish Community i OświęcimAuschwitz Jewish Center: MuseumAuschwitz Jewish Center

Valle di Casies Indice Geografia fisica | Origini del nome | Storia | Società | Amministrazione | Sport | Note | Bibliografia | Voci correlate | Altri progetti | Collegamenti esterni | Menu di navigazione46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)46°46′N 12°11′E / 46.766667°N 12.183333°E46.766667; 12.183333 (Valle di Casies)Sito istituzionaleAstat Censimento della popolazione 2011 - Determinazione della consistenza dei tre gruppi linguistici della Provincia Autonoma di Bolzano-Alto Adige - giugno 2012Numeri e fattiValle di CasiesDato IstatTabella dei gradi/giorno dei Comuni italiani raggruppati per Regione e Provincia26 agosto 1993, n. 412Heraldry of the World: GsiesStatistiche I.StatValCasies.comWikimedia CommonsWikimedia CommonsValle di CasiesSito ufficialeValle di CasiesMM14870458910042978-6

Typsetting diagram chases (with TikZ?) Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)How to define the default vertical distance between nodes?Draw edge on arcNumerical conditional within tikz keys?TikZ: Drawing an arc from an intersection to an intersectionDrawing rectilinear curves in Tikz, aka an Etch-a-Sketch drawingLine up nested tikz enviroments or how to get rid of themHow to place nodes in an absolute coordinate system in tikzCommutative diagram with curve connecting between nodesTikz with standalone: pinning tikz coordinates to page cmDrawing a Decision Diagram with Tikz and layout manager