Using the same variable across multiple scriptsCheck if a directory exists in a shell scriptGet the source directory of a Bash script from within the script itselfHow do I iterate over a range of numbers defined by variables in Bash?How do I prompt for Yes/No/Cancel input in a Linux shell script?How to check if a program exists from a Bash script?How to declare and use boolean variables in shell script?How to check if a variable is set in Bash?How to concatenate string variables in BashHow to set a variable to the output of a command in Bash?Check existence of input argument in a Bash shell script

Varistor? Purpose and principle

Is it possible to have a strip of cold climate in the middle of a planet?

How to align and center standalone amsmath equations?

What linear sensor for a keyboard?

In Star Trek IV, why did the Bounty go back to a time when whales are already rare?

Why does Async/Await work properly when the loop is inside the async function and not the other way around?

Why do IPv6 unique local addresses have to have a /48 prefix?

THT: What is a squared annular “ring”?

Proof of Lemma: Every nonzero integer can be written as a product of primes

Why has "pence" been used in this sentence, not "pences"?

If a character with the Alert feat rolls a crit fail on their Perception check, are they surprised?

Should I stop contributing to retirement accounts?

Transformation of random variables and joint distributions

Does having a TSA Pre-Check member in your flight reservation increase the chances that everyone gets Pre-Check?

Java - What do constructor type arguments mean when placed *before* the type?

Have I saved too much for retirement so far?

Can someone explain how this makes sense electrically?

Do the concepts of IP address and network interface not belong to the same layer?

Is possible to search in vim history?

MAXDOP Settings for SQL Server 2014

What is the gram­mat­i­cal term for “‑ed” words like these?

On a tidally locked planet, would time be quantized?

Visiting the UK as unmarried couple

Can somebody explain Brexit in a few child-proof sentences?



Using the same variable across multiple scripts


Check if a directory exists in a shell scriptGet the source directory of a Bash script from within the script itselfHow do I iterate over a range of numbers defined by variables in Bash?How do I prompt for Yes/No/Cancel input in a Linux shell script?How to check if a program exists from a Bash script?How to declare and use boolean variables in shell script?How to check if a variable is set in Bash?How to concatenate string variables in BashHow to set a variable to the output of a command in Bash?Check existence of input argument in a Bash shell script













2















What is the best practice for using the same bash variable (that is NOT an environment variable) across multiple scripts which call each other sequentially? Should this variable be defined in every script or function in order to keep them atomically reusable?



For example, let's say that I have one script which sources a functions.sh script with many smaller reusable functions.



#script_1

DIR="$( cd "$( dirname "$BASH_SOURCE[0]" )" && pwd )"

country="$1"

. $DIR/functions.sh

function_1
function_2 $country

#functions.sh

DIR="$( cd "$( dirname "$BASH_SOURCE[0]" )" && pwd )"

function_1()
echo "The country is $country"


function_2()
local country="$1"

if [[ $country^^ == "UK" ]]; then
echo "Fish and Chips"
else
echo "Try local food"
fi



My question is: should the $country variable be defined locally in each atomic function in the functions.sh source file?



When those functions are called, $country is already defined in the initial script, so I wouldn't have to pass it on as a parameter. I'm thinking that each of those scripts should not depend on some variable maybe being defined previously, but they should be given all the information they require via parameters, so that they can be reused easily.



Also, should it be defined as country or COUNTRY?










share|improve this question









New contributor




Vlad-Octavian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 4





    This is the same as using global variables in any other language. You should try to avoid them, parameters are generally better.

    – Barmar
    Mar 21 at 9:57






  • 3





    All-uppercase variables are conventionally reserved for environment variables.

    – Barmar
    Mar 21 at 9:58






  • 1





    @Barmar so to sum up, you are saying: keep the variable in lowercase (since it's not an environment variable) and it's best to define and pass the variable as a parameter in each function instead of relying on it already existing in the context in which the script is run.

    – Vlad-Octavian
    Mar 21 at 11:51






  • 3





    I'd go further and tell you not to use ALLCAPS_VARS at all. It's too easy to use PATH=/path/to/file; DIR=$(dirname "$PATH") and then you wonder why your script suddenly emits "dirname: not found"

    – glenn jackman
    Mar 21 at 12:25
















2















What is the best practice for using the same bash variable (that is NOT an environment variable) across multiple scripts which call each other sequentially? Should this variable be defined in every script or function in order to keep them atomically reusable?



For example, let's say that I have one script which sources a functions.sh script with many smaller reusable functions.



#script_1

DIR="$( cd "$( dirname "$BASH_SOURCE[0]" )" && pwd )"

country="$1"

. $DIR/functions.sh

function_1
function_2 $country

#functions.sh

DIR="$( cd "$( dirname "$BASH_SOURCE[0]" )" && pwd )"

function_1()
echo "The country is $country"


function_2()
local country="$1"

if [[ $country^^ == "UK" ]]; then
echo "Fish and Chips"
else
echo "Try local food"
fi



My question is: should the $country variable be defined locally in each atomic function in the functions.sh source file?



When those functions are called, $country is already defined in the initial script, so I wouldn't have to pass it on as a parameter. I'm thinking that each of those scripts should not depend on some variable maybe being defined previously, but they should be given all the information they require via parameters, so that they can be reused easily.



Also, should it be defined as country or COUNTRY?










share|improve this question









New contributor




Vlad-Octavian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 4





    This is the same as using global variables in any other language. You should try to avoid them, parameters are generally better.

    – Barmar
    Mar 21 at 9:57






  • 3





    All-uppercase variables are conventionally reserved for environment variables.

    – Barmar
    Mar 21 at 9:58






  • 1





    @Barmar so to sum up, you are saying: keep the variable in lowercase (since it's not an environment variable) and it's best to define and pass the variable as a parameter in each function instead of relying on it already existing in the context in which the script is run.

    – Vlad-Octavian
    Mar 21 at 11:51






  • 3





    I'd go further and tell you not to use ALLCAPS_VARS at all. It's too easy to use PATH=/path/to/file; DIR=$(dirname "$PATH") and then you wonder why your script suddenly emits "dirname: not found"

    – glenn jackman
    Mar 21 at 12:25














2












2








2








What is the best practice for using the same bash variable (that is NOT an environment variable) across multiple scripts which call each other sequentially? Should this variable be defined in every script or function in order to keep them atomically reusable?



For example, let's say that I have one script which sources a functions.sh script with many smaller reusable functions.



#script_1

DIR="$( cd "$( dirname "$BASH_SOURCE[0]" )" && pwd )"

country="$1"

. $DIR/functions.sh

function_1
function_2 $country

#functions.sh

DIR="$( cd "$( dirname "$BASH_SOURCE[0]" )" && pwd )"

function_1()
echo "The country is $country"


function_2()
local country="$1"

if [[ $country^^ == "UK" ]]; then
echo "Fish and Chips"
else
echo "Try local food"
fi



My question is: should the $country variable be defined locally in each atomic function in the functions.sh source file?



When those functions are called, $country is already defined in the initial script, so I wouldn't have to pass it on as a parameter. I'm thinking that each of those scripts should not depend on some variable maybe being defined previously, but they should be given all the information they require via parameters, so that they can be reused easily.



Also, should it be defined as country or COUNTRY?










share|improve this question









New contributor




Vlad-Octavian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












What is the best practice for using the same bash variable (that is NOT an environment variable) across multiple scripts which call each other sequentially? Should this variable be defined in every script or function in order to keep them atomically reusable?



For example, let's say that I have one script which sources a functions.sh script with many smaller reusable functions.



#script_1

DIR="$( cd "$( dirname "$BASH_SOURCE[0]" )" && pwd )"

country="$1"

. $DIR/functions.sh

function_1
function_2 $country

#functions.sh

DIR="$( cd "$( dirname "$BASH_SOURCE[0]" )" && pwd )"

function_1()
echo "The country is $country"


function_2()
local country="$1"

if [[ $country^^ == "UK" ]]; then
echo "Fish and Chips"
else
echo "Try local food"
fi



My question is: should the $country variable be defined locally in each atomic function in the functions.sh source file?



When those functions are called, $country is already defined in the initial script, so I wouldn't have to pass it on as a parameter. I'm thinking that each of those scripts should not depend on some variable maybe being defined previously, but they should be given all the information they require via parameters, so that they can be reused easily.



Also, should it be defined as country or COUNTRY?







bash shell






share|improve this question









New contributor




Vlad-Octavian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Vlad-Octavian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 21 at 14:44







Vlad-Octavian













New contributor




Vlad-Octavian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 21 at 9:55









Vlad-OctavianVlad-Octavian

112




112




New contributor




Vlad-Octavian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Vlad-Octavian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Vlad-Octavian is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 4





    This is the same as using global variables in any other language. You should try to avoid them, parameters are generally better.

    – Barmar
    Mar 21 at 9:57






  • 3





    All-uppercase variables are conventionally reserved for environment variables.

    – Barmar
    Mar 21 at 9:58






  • 1





    @Barmar so to sum up, you are saying: keep the variable in lowercase (since it's not an environment variable) and it's best to define and pass the variable as a parameter in each function instead of relying on it already existing in the context in which the script is run.

    – Vlad-Octavian
    Mar 21 at 11:51






  • 3





    I'd go further and tell you not to use ALLCAPS_VARS at all. It's too easy to use PATH=/path/to/file; DIR=$(dirname "$PATH") and then you wonder why your script suddenly emits "dirname: not found"

    – glenn jackman
    Mar 21 at 12:25













  • 4





    This is the same as using global variables in any other language. You should try to avoid them, parameters are generally better.

    – Barmar
    Mar 21 at 9:57






  • 3





    All-uppercase variables are conventionally reserved for environment variables.

    – Barmar
    Mar 21 at 9:58






  • 1





    @Barmar so to sum up, you are saying: keep the variable in lowercase (since it's not an environment variable) and it's best to define and pass the variable as a parameter in each function instead of relying on it already existing in the context in which the script is run.

    – Vlad-Octavian
    Mar 21 at 11:51






  • 3





    I'd go further and tell you not to use ALLCAPS_VARS at all. It's too easy to use PATH=/path/to/file; DIR=$(dirname "$PATH") and then you wonder why your script suddenly emits "dirname: not found"

    – glenn jackman
    Mar 21 at 12:25








4




4





This is the same as using global variables in any other language. You should try to avoid them, parameters are generally better.

– Barmar
Mar 21 at 9:57





This is the same as using global variables in any other language. You should try to avoid them, parameters are generally better.

– Barmar
Mar 21 at 9:57




3




3





All-uppercase variables are conventionally reserved for environment variables.

– Barmar
Mar 21 at 9:58





All-uppercase variables are conventionally reserved for environment variables.

– Barmar
Mar 21 at 9:58




1




1





@Barmar so to sum up, you are saying: keep the variable in lowercase (since it's not an environment variable) and it's best to define and pass the variable as a parameter in each function instead of relying on it already existing in the context in which the script is run.

– Vlad-Octavian
Mar 21 at 11:51





@Barmar so to sum up, you are saying: keep the variable in lowercase (since it's not an environment variable) and it's best to define and pass the variable as a parameter in each function instead of relying on it already existing in the context in which the script is run.

– Vlad-Octavian
Mar 21 at 11:51




3




3





I'd go further and tell you not to use ALLCAPS_VARS at all. It's too easy to use PATH=/path/to/file; DIR=$(dirname "$PATH") and then you wonder why your script suddenly emits "dirname: not found"

– glenn jackman
Mar 21 at 12:25






I'd go further and tell you not to use ALLCAPS_VARS at all. It's too easy to use PATH=/path/to/file; DIR=$(dirname "$PATH") and then you wonder why your script suddenly emits "dirname: not found"

– glenn jackman
Mar 21 at 12:25













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
);



);






Vlad-Octavian is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55277735%2fusing-the-same-variable-across-multiple-scripts%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








Vlad-Octavian is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Vlad-Octavian is a new contributor. Be nice, and check out our Code of Conduct.












Vlad-Octavian is a new contributor. Be nice, and check out our Code of Conduct.











Vlad-Octavian is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • 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%2fstackoverflow.com%2fquestions%2f55277735%2fusing-the-same-variable-across-multiple-scripts%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

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript