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

Multi tool use
Multi tool use

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







qKKmd3B3pj ipHCZktMdgnpFVRBfYKIHnrRqu9r5mdLj9k2ASXmCXNN 7Czc f AGZnqq0X5HiX
ZChbmIJwt2uX6kCmpv3MPKUsb fl,pHvxiJq Jov

Popular posts from this blog

What is the result of assigning to std::vector::begin()? The Next CEO of Stack OverflowWhat are the differences between a pointer variable and a reference variable in C++?What does the explicit keyword mean?Concatenating two std::vectorsHow to find out if an item is present in a std::vector?Why is “using namespace std” considered bad practice?What is the “-->” operator in C++?What is the easiest way to initialize a std::vector with hardcoded elements?What is The Rule of Three?What are the basic rules and idioms for operator overloading?Why are std::begin and std::end “not memory safe”?

Creating centerline of river in QGIS? The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)Finding centrelines from polygons in QGIS?Splitting line into two lines with GRASS GIS?Centroid of the equator and a pointpostgis: problems creating flow direction polyline; not all needed connections are drawnhow to make decent sense from scattered river depth measurementsQGIS Interpolation on Curved Grid (River DEMs)How to create automatic parking baysShortest path creation between two linesclipping layer using query builder in QGISFinding which side of closest polyline point lies on in QGIS?Create centerline from multi-digitized roadway lines Qgis 2.18Getting bathymetric contours confined only within river banks using QGIS?

SQL Server 2016 - excessive memory grant warning on poor performing query The Next CEO of Stack OverflowFix for slow SQL_INLINE_TABLE_VALUED_FUNCTIONLarge memory grant requestsPoor performing Query -Tsql execution plan - estimated number of rows =1 Paste the PlanMSSQL - Query had to wait for memory grantRow estimates always too lowBad performance using “NOT IN”Warning about memory “Excessive Grant” in the query plan - how to find out what is causing it?Optimizing table valued function SQL ServerWhen does SQL Server warn about an Excessive Memory Grant?Warning in Execution Plan