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;








2















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.










share|improve this question




























    2















    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.










    share|improve this question
























      2












      2








      2








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 8 hours ago









      jonny789jonny789

      139110




      139110




















          2 Answers
          2






          active

          oldest

          votes


















          5














          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





          share|improve this answer























          • 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


















          2














          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





          share|improve this answer























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









            5














            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





            share|improve this answer























            • 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















            5














            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





            share|improve this answer























            • 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













            5












            5








            5







            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





            share|improve this answer













            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






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 8 hours ago









            Jeff SchallerJeff Schaller

            46k1165150




            46k1165150












            • 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

















            • 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
















            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













            2














            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





            share|improve this answer



























              2














              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





              share|improve this answer

























                2












                2








                2







                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





                share|improve this answer













                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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 8 hours ago









                Jesse_bJesse_b

                15.7k33877




                15.7k33877



























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





















































                    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

                    Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                    Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                    François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480