Get value of the passed argument to script importing variables from another scriptWhy is printf better than echo?Sharing Variables across sub shell scriptsUpdating environment variable in a shell scriptExporting a variable so it appears in a script sourced through sudoHow to add an word for a line where transpose has doneReusing grep outputImporting environment variables settings from another serverImporting environment variables from another script into the current shellI/O error when passing command line argument to a shell scriptColour Errors / Warnings / Information in bash scriptcreate single string argument using heredoc or other technique
What is a simple, physical situation where complex numbers emerge naturally?
Pros and cons of writing a book review?
What is the best option to connect old computer to modern TV
Get value of the passed argument to script importing variables from another script
How should I push back against my job assigning "homework"?
Is it grammatical to use "car" like this?
Can a class take a different class's spell in their ritual book?
How can I determine the spell save DC of a monster/NPC?
How can I add depth to my story or how do I determine if my story already has depth?
If Sweden was to magically float away, at what altitude would it be visible from the southern hemisphere?
Concise way to draw this pyramid
Is it legal in the UK for politicians to lie to the public for political gain?
Word for a small burst of laughter that can't be held back
Is the capacitor drawn or wired wrongly?
Why use water tanks from a retired Space Shuttle?
Could a guilty Boris Johnson be used to cancel Brexit?
Have powerful mythological heroes ever run away or been deeply afraid?
Setting extra bits in a bool makes it true and false at the same time
When leasing/renting out an owned property, is there a standard ratio between monthly rent and the mortgage?
How can Iron Man's suit withstand this?
What does War Machine's "Canopy! Canopy!" line mean in "Avengers: Endgame"?
What is the right way to float a home lab?
PhD student with mental health issues and bad performance
Credit card offering 0.5 miles for every cent rounded up. Too good to be true?
Get value of the passed argument to script importing variables from another script
Why is printf better than echo?Sharing Variables across sub shell scriptsUpdating environment variable in a shell scriptExporting a variable so it appears in a script sourced through sudoHow to add an word for a line where transpose has doneReusing grep outputImporting environment variables settings from another serverImporting environment variables from another script into the current shellI/O error when passing command line argument to a shell scriptColour Errors / Warnings / Information in bash scriptcreate single string argument using heredoc or other technique
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script.
Here is the script fruitcolour.sh having variables only :
apple="Red"
mango="Yellow"
orange="Orange"
pear="Green"
Here is the main script GetFruitColour.sh :
#!/bin/bash
source fruitcolour.sh
echo "The colour of " $@ " is " $@ "."
For passing apple as argument, I want to get the value of variable apple i.e. Red .
So, When I run ./GetFruitColour.sh apple
It must give output :: The colour of apple is Red.
bash shell-script variable
add a comment |
I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script.
Here is the script fruitcolour.sh having variables only :
apple="Red"
mango="Yellow"
orange="Orange"
pear="Green"
Here is the main script GetFruitColour.sh :
#!/bin/bash
source fruitcolour.sh
echo "The colour of " $@ " is " $@ "."
For passing apple as argument, I want to get the value of variable apple i.e. Red .
So, When I run ./GetFruitColour.sh apple
It must give output :: The colour of apple is Red.
bash shell-script variable
add a comment |
I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script.
Here is the script fruitcolour.sh having variables only :
apple="Red"
mango="Yellow"
orange="Orange"
pear="Green"
Here is the main script GetFruitColour.sh :
#!/bin/bash
source fruitcolour.sh
echo "The colour of " $@ " is " $@ "."
For passing apple as argument, I want to get the value of variable apple i.e. Red .
So, When I run ./GetFruitColour.sh apple
It must give output :: The colour of apple is Red.
bash shell-script variable
I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script.
Here is the script fruitcolour.sh having variables only :
apple="Red"
mango="Yellow"
orange="Orange"
pear="Green"
Here is the main script GetFruitColour.sh :
#!/bin/bash
source fruitcolour.sh
echo "The colour of " $@ " is " $@ "."
For passing apple as argument, I want to get the value of variable apple i.e. Red .
So, When I run ./GetFruitColour.sh apple
It must give output :: The colour of apple is Red.
bash shell-script variable
bash shell-script variable
asked 8 hours ago
jonny789jonny789
139110
139110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.
To demonstrate:
apple="Red"
var="apple"
echo "$!var"
Results in:
Red
Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.
As a result, your GetFruitColour.sh script could look like:
#!/bin/bash
source ./fruitcolour.sh
for arg in "$@"
do
printf 'The colour of %s is %s.n' "$arg" "$!arg"
done
I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).
I've also changed echo to printf.
The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:
$ ./GetFruitColour.sh apple mango
The colour of apple is Red.
The colour of mango is Yellow.
Do note that there's no error-checking here:
$ ./GetFruitColour.sh foo
The colour of foo is .
You may find it easier to use an associative array:
declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'
for arg in "$@"
do
if [ "$fruits["$arg"]-unset" = "unset" ]
then
echo "I do not know the color of $arg"
else
printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
fi
done
That's the most concise explanation of!varthat I've seen.
– Tim Kennedy
8 hours ago
It worked great. Thank you for explaining.
– jonny789
7 hours ago
add a comment |
You need to use an indirect variable reference:
If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
fruitcolor.sh:
#!/bin/bash
source fruitcolor.sh
echo "The color of $1 is $!1"
$ ./getfruitcolor.sh apple
The color of apple is Red
add a comment |
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
);
);
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%2funix.stackexchange.com%2fquestions%2f521996%2fget-value-of-the-passed-argument-to-script-importing-variables-from-another-scri%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.
To demonstrate:
apple="Red"
var="apple"
echo "$!var"
Results in:
Red
Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.
As a result, your GetFruitColour.sh script could look like:
#!/bin/bash
source ./fruitcolour.sh
for arg in "$@"
do
printf 'The colour of %s is %s.n' "$arg" "$!arg"
done
I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).
I've also changed echo to printf.
The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:
$ ./GetFruitColour.sh apple mango
The colour of apple is Red.
The colour of mango is Yellow.
Do note that there's no error-checking here:
$ ./GetFruitColour.sh foo
The colour of foo is .
You may find it easier to use an associative array:
declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'
for arg in "$@"
do
if [ "$fruits["$arg"]-unset" = "unset" ]
then
echo "I do not know the color of $arg"
else
printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
fi
done
That's the most concise explanation of!varthat I've seen.
– Tim Kennedy
8 hours ago
It worked great. Thank you for explaining.
– jonny789
7 hours ago
add a comment |
One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.
To demonstrate:
apple="Red"
var="apple"
echo "$!var"
Results in:
Red
Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.
As a result, your GetFruitColour.sh script could look like:
#!/bin/bash
source ./fruitcolour.sh
for arg in "$@"
do
printf 'The colour of %s is %s.n' "$arg" "$!arg"
done
I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).
I've also changed echo to printf.
The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:
$ ./GetFruitColour.sh apple mango
The colour of apple is Red.
The colour of mango is Yellow.
Do note that there's no error-checking here:
$ ./GetFruitColour.sh foo
The colour of foo is .
You may find it easier to use an associative array:
declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'
for arg in "$@"
do
if [ "$fruits["$arg"]-unset" = "unset" ]
then
echo "I do not know the color of $arg"
else
printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
fi
done
That's the most concise explanation of!varthat I've seen.
– Tim Kennedy
8 hours ago
It worked great. Thank you for explaining.
– jonny789
7 hours ago
add a comment |
One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.
To demonstrate:
apple="Red"
var="apple"
echo "$!var"
Results in:
Red
Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.
As a result, your GetFruitColour.sh script could look like:
#!/bin/bash
source ./fruitcolour.sh
for arg in "$@"
do
printf 'The colour of %s is %s.n' "$arg" "$!arg"
done
I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).
I've also changed echo to printf.
The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:
$ ./GetFruitColour.sh apple mango
The colour of apple is Red.
The colour of mango is Yellow.
Do note that there's no error-checking here:
$ ./GetFruitColour.sh foo
The colour of foo is .
You may find it easier to use an associative array:
declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'
for arg in "$@"
do
if [ "$fruits["$arg"]-unset" = "unset" ]
then
echo "I do not know the color of $arg"
else
printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
fi
done
One way to accomplish this is through indirection -- referring to another variable from the value of the first variable.
To demonstrate:
apple="Red"
var="apple"
echo "$!var"
Results in:
Red
Because bash first takes !var to mean the value of the var variable, which is then interpreted via $apple and turned into Red.
As a result, your GetFruitColour.sh script could look like:
#!/bin/bash
source ./fruitcolour.sh
for arg in "$@"
do
printf 'The colour of %s is %s.n' "$arg" "$!arg"
done
I've made the path to the sourced script relative instead of bare, to make it clearer where the file is (if the given filename does not contain a slash, shells will search the $PATH variable for it, which may surprise you).
I've also changed echo to printf.
The functional change is to use the looping variable $arg and the indirect expansion of it to produce the desired values:
$ ./GetFruitColour.sh apple mango
The colour of apple is Red.
The colour of mango is Yellow.
Do note that there's no error-checking here:
$ ./GetFruitColour.sh foo
The colour of foo is .
You may find it easier to use an associative array:
declare -A fruits='([orange]="Orange" [apple]="Red" [mango]="Yellow" [pear]="Green" )'
for arg in "$@"
do
if [ "$fruits["$arg"]-unset" = "unset" ]
then
echo "I do not know the color of $arg"
else
printf 'The colour of %s is %s.n' "$arg" "$fruits["$arg"]"
fi
done
answered 8 hours ago
Jeff Schaller♦Jeff Schaller
46k1165150
46k1165150
That's the most concise explanation of!varthat I've seen.
– Tim Kennedy
8 hours ago
It worked great. Thank you for explaining.
– jonny789
7 hours ago
add a comment |
That's the most concise explanation of!varthat I've seen.
– Tim Kennedy
8 hours ago
It worked great. Thank you for explaining.
– jonny789
7 hours ago
That's the most concise explanation of
!var that I've seen.– Tim Kennedy
8 hours ago
That's the most concise explanation of
!var that I've seen.– Tim Kennedy
8 hours ago
It worked great. Thank you for explaining.
– jonny789
7 hours ago
It worked great. Thank you for explaining.
– jonny789
7 hours ago
add a comment |
You need to use an indirect variable reference:
If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
fruitcolor.sh:
#!/bin/bash
source fruitcolor.sh
echo "The color of $1 is $!1"
$ ./getfruitcolor.sh apple
The color of apple is Red
add a comment |
You need to use an indirect variable reference:
If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
fruitcolor.sh:
#!/bin/bash
source fruitcolor.sh
echo "The color of $1 is $!1"
$ ./getfruitcolor.sh apple
The color of apple is Red
add a comment |
You need to use an indirect variable reference:
If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
fruitcolor.sh:
#!/bin/bash
source fruitcolor.sh
echo "The color of $1 is $!1"
$ ./getfruitcolor.sh apple
The color of apple is Red
You need to use an indirect variable reference:
If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion.
fruitcolor.sh:
#!/bin/bash
source fruitcolor.sh
echo "The color of $1 is $!1"
$ ./getfruitcolor.sh apple
The color of apple is Red
answered 8 hours ago
Jesse_bJesse_b
15.7k33877
15.7k33877
add a comment |
add a comment |
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.
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%2funix.stackexchange.com%2fquestions%2f521996%2fget-value-of-the-passed-argument-to-script-importing-variables-from-another-scri%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