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 grammatical 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
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
New contributor
add a comment |
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
New contributor
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 usePATH=/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
add a comment |
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
New contributor
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
bash shell
New contributor
New contributor
edited Mar 21 at 14:44
Vlad-Octavian
New contributor
asked Mar 21 at 9:55
Vlad-OctavianVlad-Octavian
112
112
New contributor
New contributor
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 usePATH=/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
add a comment |
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 usePATH=/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
add a comment |
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.
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%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.
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.
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%2fstackoverflow.com%2fquestions%2f55277735%2fusing-the-same-variable-across-multiple-scripts%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
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